Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. DialogBox.show() issue in MainWindow [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

DialogBox.show() issue in MainWindow [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 8.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      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()

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          Try this :

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

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vezprog
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              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?

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • EddyE Offline
                EddyE Offline
                Eddy
                wrote on last edited by
                #7

                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

                Qt Certified Specialist
                www.edalsolutions.be

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved