Buttons in vertical layout in QDialog with return value - qt

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.

Related

How to set cursor when it is in some area?

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.

QCompleter popup position

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...)

How to modify QCombobox current item using PaintEvent() in Qt

I have a Combobox which has 3 items i.e. Item 1, Item 2 and item 3. I added these 3 items in .ui file and also added an image each for all using a resource file. Now when I am running the application, it shows the first item in combobox along with a image towards left. When i click on dropdown, I can see all the items along with images.
What I basically want to do is just display the image in the combobox and when user clicks on dropdown, it should show the image and text too.
Scenario: Items and images inside Combobox are added as follows:
Image 1 Item 1
Image 2 Item 2
Image 3 Item 3
When I run the app, it should display in combobox as
Image1
since first item gets displayed by default. I think it can be done when painting the current selection, QCombobox uses a re-implantation : void MyCombo::paintEvent(QPaintEvent *e) which is something I am not aware of.
Can anyone help me in this?? :)
How can I achieve it???
You hould inherit a QComboBox and reimplement void QComboBox::showPopup () [virtual]
P.S. If you reimplement this function to show a custom pop-up, make sure you call hidePopup() to reset the internal state.
Second idea I have is to implement a model for combobox but i don't now exatly is there any roule for popup elements.

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 make a QLabel turn to be a combobox or edit control, when you clicked this label

It looks like a label normally, once you move the mouse over it, or clicked it, then will turn to be a edit control or combobox likewise.
Use a QStackedWidget. According to the docs, "The QStackedWidget class provides a stack of widgets where only one widget is visible at a time." You can then reimplement QWidget::enterEvent and QWidget::leaveEvent to detect mouse overs and show the appropriate widget.

Resources