Qt - Radiobutton in diff dialog(window) -> taking result to the mainwindow into lineedit - qt

my problem looks like that. I got dialog window and i know how to get result from checked radiobutton but only in this window. How to take result into different window(mainwindow).
button(Pobierz) is on mainwindow and close to this button is lineedit2 where i would like to take result from checked radiobutton, but dont know how. I make lineedit in this new Dialog window and its taking result, but i dont know how to take this result into mainwindow. Hope I explain good enough. Thanks for any help.
void Pobierz::on_pushButton_clicked()
{
if(ui->radioButton1->isChecked())
{
ui->lineEdit->setText("K");
}
if(ui->radioButton2->isChecked())
{
ui->lineEdit->setText("S");
}
if(ui->radioButton3->isChecked())
{
ui->lineEdit->setText("I");
}
}
Greetings,Tom.
up1
i tried couple ways but still cant solve this..

In your class for the dialog, declare a signal that passes a QString. Also override the accept function (if you don't already). In the accept function, emit the signal with the appropriate string according to the radio buttons. (Don't forget to call the parent accept function in your own).
In your class for the main window, when you create the dialog, connect the signal from the dialog to a slot that sets the text in the line edit in the main window. When the dialog is accepted, the signal should fire, running the slot in the main dialog, adding the appropriate text to the line edit.

Create a slot in the main window which would get the radio button status from the dialog window. In this slot set your main window's lineedit based on the result from the dialog window radio button checked status.
While creating the radio button on the dialog window, connect the radio button's clicked signal with the main window's slot defined earlier.

Related

What SIGNAL is emitted in Qt when pressing the "X" on the top right corner to terminate the Window?

Which SIGNAL is emitted, when pressing the "X" in the top right corner of a Qt Window application?
I have a second QDialog widget beside my MainWindow in my Qt application. And I would like to intercept the press on the "X" in my second QDialog, how?
The QCloseEvent class contains parameters that describe a close event. Close events are sent to widgets that the user wants to close, usually by choosing "Close" from the window menu, or by clicking the X title bar button. They are also sent when you call QWidget::close() to close a widget programmatically.
To ignore it you can call ignore() method:
void YourDialog::closeEvent(QCloseEvent* iEvent)
{
// ignore close event
iEvent->ignore();
}
There is no signal for that, you need to reimplement QWidget::closeEvent() or install an event filter filtering for QCloseEvents.

Qt press left click twice for context menu to disappear

I'm trying to use a context menu from Qt when I press right click.
Here is what I've tried:
connect(mtreeView, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(showContextMenu(const QPoint&)));
Then
void MainTreeViewController::showContextMenu(const QPoint& pos)
{
QPoint globalPos = mtreeView->mapToGlobal(pos);
QMenu rightClickMenu;
rightClickMenu.addAction(QString("Option"));
rightClickMenu.exec(globalPos);
}
When I press right click, the menu appears. Then if I press left click outside of it nothing happens. I must press left click twice in order to make the menu disappear.
Why does that happen? Thanks!
This can happen if showContextMenu is called twice for a single right click. You can verify by setting a breakpoint in showContextMenu and checking whether it is called twice.
Probably your signal slot connection is created twice, which can be the reason behind this. You can verify by setting a breakpoint to the line where signal slot connection is made.

qt create dialog

I've made my first qt window. Now I'd like to make my first dialog, using qt. I have just finished creating the dialog, which is basically made of a QDialogButtonBox, and now I'd like to connect it to the window. I have two beginner's questions:
How can I retrieve how the dialog was closed (ok pressed or cancel pressed) from the window.cpp, which creates a new dialog, and then calls dialog->show() ?
Where and how to destroy the dialog pointer ?
If you use dialog->show() then I assume it's non-modal dialog.
If you have created QDialogButtonBox and connected its signals with accept() and reject() slots of your dialog as documentation shows, then your dialog will emit finished(int) and additionally accepted() or rejected() signals by which you can determine how it was closed.
If you need more customized behavior, then you can reimplement closeEvent(QCloseEvent *event) or create your own singnals.
If you need to delete your dialog you can use setAttribute(Qt::WA_DeleteOnClose, true);, which will delete instance on close.
you can use one flag, and signal and slot.
when put OK flag=1 , and when put cancel then flag=-1; and then use signal.
in in the window.h write code how to handle that flags with 1 simple slot.
for destroying the pointer you can use signal and slot in your Dialog and tell when user push
Ok, or Cancel , or exit (up- right (red cross)) go to slot in call the Destructer of dialog
and also you that you better set parent of dialog to window.
First Question:
When you want to show the dialog,just construct it,using myDialog *d = new myDialog(this)(the this pointer will make sure that you havn't to delete the pointer you created 'cause Qt will handle this if you specified the dialog's parent). And use d->exec() if you need a modal dialog, or d->show() to make it non-modal;
Second Question:
Once your specified the dialog's parent object, all u need is just use it and leave alone the memory managent,Qt will do this for you. Also you can use d->setAttribute(Qt::WA_DeleteOnClose,true) to make it destroy itself when it is closed.
Remember to link the QDialogButtonBox to your dialog's actions.

Shape changing Dialog Box

I was trying to use a shape changing dialog box i.e., when I click on a button the size of the dialog box should become big with the extra details. In order to do that I wrote the following code on button:
QObject::connect(ui->moreButton, SIGNAL(toggled(bool)),
ui->sgroupBox, SLOT(setVisible(bool)));
but there are no changes happening on my dialog box. What should I do in this case.
I had hidden the extra details by placing them in a grid using hide() function. The extra details are getting hidden but the size of widget is not getting changed.
Please help me with a solution
If I understand your question correctly you are trying to resize your QDialog box after clicking on a button in your user interface?
Since a QDialog inherits from a QWidget, you are able to call the QWidget::resize(int width, int height) method.
So now, to make the QDialog grow when you press the button you simply need to connect the toggled(bool) signal to a slot which then calls resize.
ie.
QObject::connect(ui->moreButton, SIGNAL(toggled(bool)), whateverClassManagesYourQDialog, onButtonToggled(bool));
Then implement this slot in your class that manages your QDialog:
ie.
// This is a slot in your class which implements QDialog
whateverClassManagesYourQDialog::onButtonToggled(bool toggledState){
ui->sGroupBox.setVisible(toggledState); // This will show or hide sGroupBox
resize(someIncrement,someIncrement); // This will grow your QDialog
}

How to remove focus from a QLineEdit when anyplace else on the window is clicked

I'm working on a custom Qt button that allows you to edit the text on the button if you double click it. When the button is double clicked, a QLineEdit appears where the text on the button is allowing the user to edit the text on the button. My requirement is that if the user clicks anywhere in the application window, the QLineEdit should disappear and cancel the edit operation. This works in some cases. Specifically, it works if I click on anything that is capable of text entry. Other portions of the window don't work as expected. I'll click on a blank portion of the application window, and the QLineEdit retains its focus. How can I remove its focus in these cases?
I've found a solution that seems to work, though I'm still open to other options if there are any. I'm using PyQt4, so my example is in python:
Create a subclass of QLineEdit just so I have a new type. I don't want or need this behavior on all QLineEdit instances; just these specific ones.
class MyLineEdit(QtGui.QLineEdit):
pass
Now, in my QMainWindow subclass, I override the mousePressEvent() implementation. It gets the currently focused widget. If that widget is of type MyLineEdit, clear the focus.
class MyMainWindow(QtGui.QMainWindow):
def ...
def mousePressEvent(self, event):
focused_widget = QtGui.QApplication.focusWidget()
if isinstance(focused_widget, MyLineEdit):
focused_widget.clearFocus()
QtGui.QMainWindow.mousePressEvent(self, event)
def ...
This gets me the behavior I'm looking for so that if the user clicks anywhere on the application's window, the focus is cleared.
Edit: I did find one caveat to this. I have a QTreeView in the main window. If the user clicks on the tree view, focus is not removed from the text edit field.
Catch the clicked() signal of your parent widget and call yourLabel->clearFocus() (that unfortunatelly happens to not be a slot, making things more complicated) there.
I followed Grant Limberg instruction here but figured out that, in my case, a simple:
QApplication.focusWidget().clearFocus()
would fix the problem.
I'm not sure if this also works in Qt4 (I'm using PyQt5) but you can change the FocusPolicy of the QMainWindow or parent widget to clear the focus in the QLineEdit. As per https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop
I've changed the policy of my QMainWindow to Qt.StrongFocus and it worked like the functionality described in the question.
If done in C++ I would do something along the lines of:
connect(myWidgets->MyLineEdit, SIGNAL(returnPressed()), this, SLOT(onLineEditDone());
void onLineEditDone()
{
myWidgets->MyLineEdit->clearFocus();
}
For this particular case I would use editingFinished() instead of returnPressed(), probably, but I would NOT use textChanged(QString).

Resources