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. How to make a window/widget keep showing?
Forum Updated to NodeBB v4.3 + New Features

How to make a window/widget keep showing?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 12.9k 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.
  • G Offline
    G Offline
    galmeida
    wrote on 19 Sept 2010, 00:33 last edited by
    #1

    Hi, I am a beginner at Qt and I'm trying to do simple things.

    I´ve created a MainWindow and then created a widget.On the menu I´ve made an "about" option, that would show program version etc.For the code, I´ve put:
    @
    void MainWindow::on_actionAbout_triggered()
    {
    aboute a;
    a.show();
    }
    @

    But differently from the mainwindow, the widget closes immediatly after opening.How can I make it open without closing?

    thanks
    [edit: fixed @ / chetankjain]

    1 Reply Last reply
    0
    • V Offline
      V Offline
      viswesr
      wrote on 19 Sept 2010, 04:03 last edited by
      #2

      Post the complete source code of your application.

      You can also refer the numerous examples that came with Qt. Every example has an about box implemented with QMessageBox.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        galmeida
        wrote on 19 Sept 2010, 04:43 last edited by
        #3

        Thank you viswesr.
        @

        //—-mainwindow.h

        #ifndef MAINWINDOW_H

        #define MAINWINDOW_H

        #include

        namespace Ui {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

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

        private:
        Ui::MainWindow *ui;

        private slots:
        void on_actionSobre_triggered();
        void on_actionSair_triggered();
        };

        #endif // MAINWINDOW_H

        //———-sobre.h(“sobre” means “about” in portuguese)—-

        #ifndef SOBRE_H

        #define SOBRE_H

        #include

        namespace Ui {
        class sobre;
        }

        class sobre : public QWidget
        {
        Q_OBJECT

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

        private:
        Ui::sobre *ui;

        private slots:
        void on_pushButton_clicked();
        };

        #endif // SOBRE_H

        //main.cpp————————————-

        #include

        #include “mainwindow.h”

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

        //mainwindow.cpp—————————————————-

        #include “mainwindow.h”

        #include “ui_mainwindow.h”

        #include “sobre.h”

        #include “ui_sobre.h”

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

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

        void MainWindow::on_actionSair_triggered()
        {
        MainWindow::close();
        }

        void MainWindow::on_actionSobre_triggered()
        {
        sobre s;
        s.show();
        }

        //-sobre.cpp——————

        #include “sobre.h”
        #include “ui_sobre.h”

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

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

        void sobre::on_pushButton_clicked()
        {
        sobre::close();
        }
        @

        the "sobre" widget contet is just text and a "ok" button that closes the window.

        I'd like to know the general procedure to keep something wich is not the main window showing while not closed by some action.

        [edit: fixed @ / chetankjain]

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on 19 Sept 2010, 08:51 last edited by
          #4

          Could you please use the code tags around your code? It is hardly readable the way it is. Thanks!

          1 Reply Last reply
          0
          • ZlatomirZ Offline
            ZlatomirZ Offline
            Zlatomir
            wrote on 19 Sept 2010, 09:12 last edited by
            #5

            You can do one of the following:

            1. You can create the dialog on the heap (using a pointer and "new", and don't forgot to parent the dialog with "this" pointer)

            2. You can create your dialog on that slot stack (like in the first post) and use a.exec() (instead of show(), it will make your dialog modal) !!!This works only if you derived your about from QDialog class

            3. "QMessageBox":http://doc.qt.nokia.com/4.6/qmessagebox.html can be another good way to do this (since you don't need that many "widgets" in this)

            https://forum.qt.io/category/41/romanian

            1 Reply Last reply
            1
            • V Offline
              V Offline
              VC15
              wrote on 20 Sept 2010, 16:41 last edited by
              #6

              Your code contains an error that causes your "About" window to disappear immediately after showing.

              @void MainWindow::on_actionSobre_triggered()
              {
              sobre s;
              s.show();
              }@

              You create a sobre object on the stack, then call show(). After the on_actionSobre_triggered() finishes and the destructor of s is called. That's why your widget disappears immediately.

              To get rid of this create sobre's instance dynamically with operator new.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                galmeida
                wrote on 20 Sept 2010, 20:11 last edited by
                #7

                thank you very much Zlatomir and VC15

                1 Reply Last reply
                0

                1/7

                19 Sept 2010, 00:33

                • Login

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