0

I am trying to create a dialog that pops up that gives the user a selection of 6 different options. I read the documentation on QDialogButtonBox but still am a little bit confused on how to implement it. Right now I have a QInputDialog that takes in the player’s names and after the user enters that information, I want to prompt each player with this button box. After they select what they want, it stores the selected index into a variable and I can use that info to assign them pieces accordingly (not sure if this is the way to do it or ButtonBox returns a value that i can directly use to assign a player a piece).

MY code:

      hatbutton = new QPushButton(tr("Hat"));
      hatbutton->setDefault(true);

      carbutton = new QPushButton(tr("Car"));
      carbutton ->setDefault(true);

      spaceshipbutton = new QPushButton(tr("Spaceship"));
      spaceshipbutton->setDefault(true);

      basketballbutton = new QPushButton(tr("Basketball"));
      basketballbutton -> setDefault(true);

      ringbutton = new QPushButton(tr("Ring"));
      ringbutton -> setDefault(true);

      shoebutton = new QPushButton(tr("Shoe"));
      shoebutton ->setDefault(true);


      pieces = new QDialogButtonBox(this);
      pieces -> addButton(hatbutton, QDialogButtonBox::ActionRole);
      pieces -> addButton(carbutton, QDialogButtonBox::ActionRole);
      pieces -> addButton(spaceshipbutton, QDialogButtonBox::ActionRole);
      pieces -> addButton(basketballbutton, QDialogButtonBox::ActionRole);
      pieces -> addButton(ringbutton, QDialogButtonBox::ActionRole);
      pieces -> addButton(shoebutton, QDialogButtonBox::ActionRole);

Right now the issue is the button box is not even popping up. If anyone can teach me how to properly implement it, first by making it appear then by storing the values somehow it would help me tremendously. Thank you so much.

Please let me know if you need more info to help me implement this. I am also running qt 4.8

EDIT: to refine my question, is it possible to create a dialog that is both a combination of a qinputdialog and a qcombobox? I would like these dialogs to all open first before my mainwindow is displayed.

3 Answers3

2

You can create your own dialog by subclassing QDialog.

Here is an example:

mydialog.h

#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>

class QLineEdit;
class QComboBox;

class MyDialog : public QDialog
{
    Q_OBJECT
public:
    explicit MyDialog(QWidget *parent = 0);
    QString getName() const;
    int getIndex() const;

private:
    QComboBox *comboBox;
    QLineEdit *lineEdit;
};

#endif // MYDIALOG_H

mydialog.cpp

#include "mydialog.h"
#include <QDialogButtonBox>
#include <QLayout>
#include <QComboBox>
#include <QLineEdit>


MyDialog::MyDialog(QWidget *parent) :
    QDialog(parent)
{
    QVBoxLayout *layout = new QVBoxLayout;
    this->setLayout(layout);

    comboBox = new QComboBox; // create combo box and add items to it
    QStringList items = QStringList() << "item1" << "item2" << "item3" << "item4";
    comboBox->addItems(items);
    layout->addWidget(comboBox);

    lineEdit = new QLineEdit; // create line edit
    layout->addWidget(lineEdit);

    // create button box
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    layout->addWidget(buttonBox);
}

QString MyDialog::getName() const
{
    return lineEdit->text();
}

int MyDialog::getIndex() const
{
    return comboBox->currentIndex();
}

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include "mydialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    MyDialog myDialog;
    if(myDialog.exec()) // shows MyDialog on the screen and waits for the user to close the dialog
    {
        int index = myDialog.getIndex();
        QString name = myDialog.getName();
        // do something with these..
        w.setPlayerData(index, name);
    }
    else
        return 0;

    w.show(); // shows MainWindow on the screen
    return a.exec();
}

Add a function like this in your MainWindow class:

void MainWindow::setPlayerData(int _index, const QString &_name)
{
    index = _index; // index is a member variable declared somewhere in your mainwindow.h
    name = _name; // name is a member variable declared somewhere in your mainwindow.h
}
thuga
  • 12,601
  • 42
  • 52
  • Wow that makes everything alot more clear. I now need to take the index number and Qstring that I got from the user and pass it into the mainwindow. How could I pass data from one dialog into my mainwindow? sorry for the basic questions and I really appreciate the help – user3587954 Apr 30 '14 at 09:10
  • @user3587954 You can create a function in your `MainWindow` class that takes a `QString` and an `int` as arguments, and pass those values to it. I will modify my example to show you. – thuga Apr 30 '14 at 09:12
  • alright I'll give it a shot, im a little confused about the order in which the objects are declared. If I want to obtain the user input before I print the mainwindow, shouldnt I declare the mainwindow after I create the dialog object? – user3587954 Apr 30 '14 at 09:22
  • @user3587954 It doesn't matter if you create `MainWindow` before or after `MyDialog`. This is because they will not be shown until you call `show()` in `MainWindow`'s case, or `exec()` in `MyDialog`'s case. You can change the order of creation if it makes things more clear to you. – thuga Apr 30 '14 at 09:26
  • Theoretically would it work if I created a QInputDialog inside the MyDialog class that prompted the user for how many players there were, then run a for loop based off of the # of players so that I open up a dialog and get each users info? There could be 2 - 4 players – user3587954 Apr 30 '14 at 09:35
  • @user3587954 You could modify the dialog by creating an `Add Player` button. When this button is clicked, you can store `index` and `name` in some containers, `QList` and `` for example. Or create a `struct` that has `int name` and `QString name` variables. Then add the `index` and `name` from the user input to this struct, and add the struct into a container such as `QList`. – thuga Apr 30 '14 at 09:41
  • @user3587954 But if you wanted to use the `QInputDialog` method that you mentioned above, you should create it in the `main` function and then loop the `myDialog.exec()` part as many times as there are number of players. Then of course store the player data in the `MainWindow` class for each player in some container. – thuga Apr 30 '14 at 09:44
  • perfect, I just got that implemented but could I make the box appear X amount of times (based off the amount of players). I call the dialog and it runs and I store the data but it doesnt run based off of the amount of players. Basically I created an InputDialog that appeared before the main dialog store that into an integer value, and then ran a for loop based off the integer value the user entered previously. Any idea if that method would work with some refining? – user3587954 Apr 30 '14 at 09:50
  • @user3587954 `for(int i = 0; i < num_of_players; i++) { if(myDialog.exec()) ...}` – thuga Apr 30 '14 at 09:52
  • To do that I would need to create another Dialog that prompts the user for an input. Wouldnt it just be easier if I created a QInputDialog within my dialog class or that wouldnt work? – user3587954 Apr 30 '14 at 09:58
  • @user3587954 Technically it's not creating a new dialog each time, but showing the same dialog over and over. But if you want to implement it inside your dialog, you will need to connect the `accepted` signal of `buttonBox` to your own slot instead of the `accept` slot. Then in this slot check how many players have been added, and if it's the amount needed, just call `accept()`. – thuga Apr 30 '14 at 10:05
0

You need to add the QDialogButtonBox to your window's central widget. I guess this is the answer that should help you forward: How to add buttons to a main window in Qt?

Community
  • 1
  • 1
neeohw
  • 555
  • 4
  • 12
0

The dialogbuttonbox is no dialog. The QDialogButtonBox can be/is part of the QDialog.

You can approach this two different ways.

First and much more flexible version is to subclas the QDialog and manually create your button box.

Second possibility is probably what you are trying to achive (example from our sources with default buttons in button box and lineedit):

    int type = 0;

    QDialog *d = new QDialog();
    QVBoxLayout *l = new QVBoxLayout();
    QLabel *la = new QLabel(tr("Some Question:"));
    la->setMinimumHeight(30);
    l->addWidget(la);
    QLineEdit *le = new QLineEdit();
    le->setMinimumHeight(30);
    l->addWidget(le);
    QDialogButtonBox *b = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,d);
    foreach(QPushButton* bu, b->findChildren<QPushButton*>())
        bu->setMinimumHeight(40);
    connect(b,SIGNAL(accepted()),d,SLOT(accept()));
    connect(b,SIGNAL(rejected()),d,SLOT(reject()));
    l->addWidget(b);
    d->setLayout(l);
    d->setWindowFlags(d->windowFlags() | Qt::FramelessWindowHint);
    d->setWindowModality(Qt::ApplicationModal);

    d->exec();
    if(d->result() == QDialog::Rejected)
    {
        delete le;
        delete la;
        delete l;
        delete b;
        delete d;
        return;
    }
    type = le->text().toInt();

Note: You can use QScopePointer instead of the ugly delete section. But remember, when querying elements from the dialog, the elements itself and the dialog must not yet be deleted.

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • this is exactly what i am looking for. Is it possible to add a combobox where the user can select from? How can i store the value the user selected into a variable? (does a combobox return an index number?) – user3587954 Apr 30 '14 at 08:27
  • @user3587954 [`QComboBox::currentIndex`](http://qt-project.org/doc/qt-4.8/qcombobox.html#currentIndex-prop) returns the selected index. – thuga Apr 30 '14 at 08:28
  • I am getting this error : error: no matching constructor for initialization of 'QDialogButtonBox' ...QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,pInput); ^ – user3587954 Apr 30 '14 at 08:35
  • @user3587954 Why don't you just create a new class by subclassing `QDialog`? Your code will be much cleaner that way. If you're using Qt Creator, it can generate you a dialog with an existing button box. – thuga Apr 30 '14 at 08:42
  • @thuga Is there anyway to transfer my code into qt creator if I coded everything already? I am using libraries from qt 4.8. – user3587954 Apr 30 '14 at 08:44
  • @user3587954 I'm not saying you should switch to Qt Creator just for this little issue. It's quite easy to subclass a `QDialog`. I will provide you an example. – thuga Apr 30 '14 at 08:54