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
}
Related
I want to create a QDialog with Qt that looks somewhat like this:
The desired properties here are:
I can add a text to the dialog explaining the question behind it
I can add several buttons that are in a vertical layout
I can retrieve the value of the clicked button, i.e. I know if the user cancelled or clicked 1, 2 or 3 - ideally I can emit a signal with the corresponding value as parameter.
The dialog has a certain minimum height and width.
I have used a QMessageBox before for this purpose, but I can't get it to use a vertical layout. I have experimented with a QDialogButtonBox and a QDialog, but I can't really figure out how to get my desired return value easily.
A piece of code creating this dialog with code how to retrieve the clicked value would be great!
Did you try a QDialog widget? you can add QDialog without buttons to your project. In UI designer, add Vertical layout, label, and buttons.
In your class define your signals and emit it when the user clicked on buttons.
I have some rectangle link area on my widget. What is the best way to make cursor Qt::PointingHandCursor when it is in this area?
The QWidget class has a cursor property that you can set with the cursor you wish displayed when the mouse is above it.
EDIT:
Without more detail on what you are trying to achieve, I can only assume you're making your life much more difficult than it needs to be. You can create a QLabel widget to handle the link and then place the label on the menubar automatically.
QLabel *link = new QLabel("<a href='http://doc.qt.io'>Qt Documentation</a>");
menuBar()->setCornerWidget(link);
All the text formatting, cursor display and user interactions are handled by existing code in the Qt classes. The only thing you need to do yourself is to handle what happens when the user clicks on the link, that you can do by connecting a slot to the QLabel::linkActivated(const QString &) signal.
I'm having trouble trying to move the QCompleter popup view position.
I tried the QCompeter:complete and it's pops the completer view in the position as I wanted.
But if I start typing it close it, and open the completer in the 'default' position.
I also tried the QCompleter:setPopup() function.
I create a QListView and I tried to moved to different position.
And still the QCompleter popup view remains in the same position.
In my project I'm using a QFrame that wrap QLineEdit.
And I want that the completer view will get the QFrame position.
I succeed to set the completer view width via setFixedWidth() function.
but not to move the position.
Any suggestions ?
Thanks.
I suggest setting the CompletionMode to InlineCompletion, so there will be no popup. Then make your QListView indepedant of the QLineEdit; just react to signals that indicate when a view types some text, leaves the QLineEdit, etc (hint: subclass QListView) and sets the text in QLineEdit when a user selects a value from the list.
I think it will be difficult to override the placement since QCompleter takes ownership of your QListView. (Personally I think it does not make much sense to place the completion list somewhere else than next to the input field, but alas...)
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.
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).