user3587954
user3587954

Reputation: 5

Implementing QDialogButtonBox by storing the index values that the user selects

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.

Upvotes: 0

Views: 1273

Answers (3)

thuga
thuga

Reputation: 12931

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
}

Upvotes: 2

Sebastian Lange
Sebastian Lange

Reputation: 4029

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.

Upvotes: 0

neeohw
neeohw

Reputation: 605

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?

Upvotes: 0

Related Questions