DialogBox.show() issue in MainWindow [SOLVED]



  • Right now before I fully start designing my application, I would like to make sure I am able to open a new form from my main for, and pass variables between the two. As a start, I figured I would try to at least make a widget (button) that I can click and a new form will appear.

    I have a couple questions before my I post my code (very basic). If I execute a QDialog but, will my main window functions still run in the background? 2nd, can I still use a .ui file for the designer and create my dialog box that way instead of hard coding dimensions and what not?

    Now on that now, here is the code I am trying to implement.

    dialogtest.h
    @

    #ifndef DIALOGTEST_H
    #define DIALOGTEST_H

    #include <QDialog>
    #include "mainwindow.h"

    namespace Ui {
    class dialogtest;
    }

    class dialogtest : public QDialog
    {
    Q_OBJECT

    public:
    explicit dialogtest(QWidget *parent = 0);
    ~dialogtest();

    private:
    Ui::dialogtest *ui;
    };

    #endif // DIALOGTEST_H
    @

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <dialogtest.h>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::MainWindow *ui;

    public slots:
    void openDialog();
    };

    #endif // MAINWINDOW_H
    @

    dialogtest.cpp
    @
    #include "dialogtest.h"
    #include "ui_dialogtest.h"
    #include "mainwindow.h"

    dialogtest::dialogtest(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dialogtest)
    {
    ui->setupUi(this);
    }

    dialogtest::~dialogtest()
    {
    delete ui;
    }
    @

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialogtest.h"
    #include <QDialog>

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

    connect(ui->btopen, SIGNAL(clicked()), this, SLOT(openDialog()));
    

    }

    void MainWindow::openDialog()
    {
    dialogtest.exec();
    }

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

    main.cpp
    @

    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include "dialogtest.h"

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

    return a.exec(&#41;;
    

    }
    @

    I am very new to Qt and am trying to learn on my own through tutorials so bare with me here. This is the error that flags:
    mainwindow.cpp:18: error: expected unqualified-id before ‘.’ token

    I guess my issue is linking the two forms together. My overall goal is to pass variable values between the two forms, but I figured I would start here. It might just be as simple as using global variables as in C++ using extern, but I will look into that after this issue is solved.

    Thank you in advance!


  • Moderators

    In you opendialog slot you have to make an instance of dialogTest first. Your program is complaining it doesn't know the identifier before the .

    Then use show()



  • since I have the .ui file made, and the class for it, I thought it would be all set. Do you mean in my main function in mainwindow.cpp I have to add an initialization for it? Could you elaborate a little bit please.

    Thank you again for the quick reply.


  • Moderators

    Try this :

    @void MainWindow::openDialog()
    {
    Dialogtest dia;
        dia.show();
    }@



  • For some reason .show() does not activate the window. exec() does though. Not really sure about the difference, but either way, problem solved. Thank you!


  • Moderators

    I don't think won't work, as Dialogtest is created on the stack and will fall out of scope as openDialog() exits, since show() doesn't block.

    Edit: Or will it?


  • Moderators

    Sorry, my mistake. I didn't test it first. If you use show, you should use setModal first.

    "Here is the full explanation.":http://developer.qt.nokia.com/doc/qt-4.7/qdialog.html#id-8f49114e-2fb8-4525-8a9d-af84f69df7e6


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.