You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

jjamfd

Beginner

  • "jjamfd" is male
  • "jjamfd" started this thread

Posts: 7

Location: New York City

Occupation: UNIX/Linux System Admin

  • Send private message

1

Thursday, September 24th 2009, 2:54pm

QMainwindow and QDialog - Qt Creator/Qt Designer

Hi,
I'm using Qt Creator to write a GUI that consists of a main window and a preferences dialog.

I can't seem to find any trivial examples of how to do this.

The only way I have been able to call dialog windows was from within the same class. If I do it that way, I don't seem to have a way of modifying the GUI with Qt Designer.

How do I have a separate .ui file and call it from my mainwindow?


For example, I have a mainwindow.ui and a dialog.ui. The code below displays the mainwindow.ui, but I have no clue how to call the dialog window (dialog.ui). 8|

mainwindow.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void showDialog()
{

  // whistling in the dark

}


main.cpp

Source code

1
2
3
4
5
6
7
8
9
10
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}




mainwindow.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

namespace Ui
{
    class MainWindowClass;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots;
    void showDialog();

private:
    Ui::MainWindowClass *ui;
};

#endif // MAINWINDOW_H


Some basic examples and an explanation in layman's terms would really go a long way towards helping me. I have a strong background in shell scripting, php, html, css, mysql, postgres, and perl, but Qt/C++ is proving to be a challenge.

Suggestions are also welcome.

Thanks,
-jj
"The last thing one knows when constructing a work is what to put first." - Blaise Pascal

2

Thursday, September 24th 2009, 8:10pm


jjamfd

Beginner

  • "jjamfd" is male
  • "jjamfd" started this thread

Posts: 7

Location: New York City

Occupation: UNIX/Linux System Admin

  • Send private message

3

Thursday, September 24th 2009, 11:30pm

QMainwindow and QDialog - Qt Creator/Qt Designer

Thanks for the response P, but I don't believe that is what I need.

Perhaps this example will illustrate my problem more clearly.

This is my mainwindow.cpp, it shows only a menubar with a single "actionOptions" item. I would like that menubar action to show an existing QDialog called optionsDialog which is contained in a separate file dialog.ui.

Instead, when I click on the menubar item, it shows a blank dialog box.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_dialog.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);
    connect(ui->actionOptions, SIGNAL(triggered() ), this, SLOT( showOptionsDialog() ) );
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showOptionsDialog()
{
    //The code below shows a blank dialog
    //How do I show the dialog window from my file, dialog.ui, instead?
    QDialog optionsDialog(this);
    optionsDialog.setWindowTitle(tr("Configure Options"));
    optionsDialog.exec();
}

void MainWindow::acceptOptionsChanges()
{
    //ACTION: Write the changes to config file if the user clicks OK
    QSettings settings("/options.ini",
                       QSettings::IniFormat);
    settings.setValue("testKey", "testValue");
}


This is mainwindow.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

namespace Ui
{
    class MainWindowClass;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void showOptionsDialog();
    void acceptOptionsChanges();


private:
    Ui::MainWindowClass *ui;
};

#endif // MAINWINDOW_H


main.cpp

Source code

1
2
3
4
5
6
7
8
9
10
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


Can anyone tell me what I am missing?

This is a list of all the files in this project:

exampleApp.pro
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
dialog.ui
ui_dialog.h
ui_mainwindow.h

Please let me know if I should post more of the code.

Thanks,
-JJ
"The last thing one knows when constructing a work is what to put first." - Blaise Pascal

jjamfd

Beginner

  • "jjamfd" is male
  • "jjamfd" started this thread

Posts: 7

Location: New York City

Occupation: UNIX/Linux System Admin

  • Send private message

4

Friday, September 25th 2009, 2:50am

OK, Nevermind, I was able to figure it out by studying more of the qt4 examples and reading the Qt book. The torrent client example was particularly helpful.

In short, I needed to create a dialog.cpp and dialog.h and add some includes.

If there are any other beginners like me who chance upon this thread and would like to see my code, just send me an email or IM me.
"The last thing one knows when constructing a work is what to put first." - Blaise Pascal

5

Thursday, June 28th 2012, 11:35am

@jjamfd

hie dude..........can u explain me how u sloved ur problemm....

6

Thursday, June 28th 2012, 12:57pm

mainwindow.cpp looks like this:

Source code

1
2
3
4
5
6
7
8
#include "mainwindow.h"
#include "ui_mainwindow.h" // <<<<<<<<  ui design file

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);  // <<<<<<<<<<  setup the ui.
}


But he was missing a similar dialog.cpp file that would inlcude the dialog design file, and then use the setup method.
Do as the op did and look at some examples...
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.