Using Customized Dialog Boxes
-
Hi there, I'm a bit of a noob when it comes to Qt programming, so if I say something extremely revealing of my noviceness, please forgive me.
So I have an application, and so far for all the dialogs I've just been using code directly to make dialogs.
eg:
@void Dialog::informationMessage()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE);
if (reply == QMessageBox::Ok)
informationLabel->setText(tr("OK"));
else
informationLabel->setText(tr("Escape"));
}@However, now I want to use a custom created dialog box when a button is pressed. I'm just not sure how to do it. My main function works something like this:
@
int main(int argc, char *arg[])
{
QApplication a(argc, argv);
Launcher *w = Launcher::getSingleton();
w->show();
int ret = a.exec();
return ret;
}
@My Launcher.h source file's work something like this:
@
class Launcher : public QMainWindow
{
Q_OBJECTpublic:
Launcher(QWidget *parent = 0, Qt::WFlags flags = Qt::FramelessWindowHint);
~Launcher();static Launcher *getSingleton();
//public functionsprivate:
Ui::LauncherClass ui;//rest of functions
@
Anyway from my Launcher.cpp file I want to have a function that when called, will create a new Ui::CustomDialogClass ui; and allow me to use a customized dialog box I've created, but I'm not sure exactly how I should go about doing it as this "public QMainWindow" is used by my Launcher.h and its not really a "QMainWindow" its more of a "QSubWindow". Anyway if someone could point me in the right direction that'd be super awesome. Thanks!
-
Hi and welcome to devnet,
I would recommend you to go through the examples in the Qt documentation, you'll have a good base to do what you want to achieve.
-
Thanks for the reply. However, I've been looking through some of the examples. eg: http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
Problem is all of them seem to use widget's. I just want a dialog box, not a widget. Am I confusing what Widget's actually are?
Some sample code looked promising:
@
QWidget *widget = new QWidget;
Ui::CalculatorForm ui;
ui.setupUi(widget);widget->show(); return app.exec();
@
But...I'm not so sure about the QWidget stuff. I thought I just wanted a dialog box, but it doesn't seem to allow me to do that except as a main dialogue. And apparently I can't have more than 1 of those. :( -
QDialog is a specialized QWidget, if you need a QDialog, simply use it in place of QWidget. You can also go through the examples given in QDialog's documentation, they'll show you various ways of using it.
-
So I couldn't figure out how to get QDialog's to work so I just switched to using widgets and then did this:
@ SettingDialogues *object = new SettingDialogues(this);
object->setWindowFlags(Qt::Dialog); //make the object be a dialog
object->show();@Seems to be the trick, I had to change my ui to a widget in order to get it to work. Hopefully I can figure the process out for Dialog's.
-
If you find it's hard to understand, maybe a C++ book is useful for you at present.
-
Well its not really hard to understand c++ itself, I need a Qt specific book :D. I understand c++ well enough, I'm just not use to how Qt works. I'm sure I'll get better at using it, the more I use Qt based stuff.
Regardless, I was able to get it working with QDialog's later today, it was a simple mistake really. I had just initially declared it as a QWidget in my .h file, instead of a QDialog which was causing the problem.
Thanks for y'alls help though.