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. Pop up a new window on a pushed button
QtWS25 Last Chance

Pop up a new window on a pushed button

Scheduled Pinned Locked Moved Solved General and Desktop
buttonslotsconnectwindow
6 Posts 2 Posters 42.8k Views
  • 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.
  • A Offline
    A Offline
    Adrian.Aioanei
    wrote on 10 Feb 2017, 18:06 last edited by
    #1

    Hey,

    I am trying to develop an application than on a pushed button pop up a new window with same labels and the labels are populated from a text file.
    The problem is when i try to close the main window and open the second because in the new open window labels are not populated with the data from file.

    The second window is populated if in the main function i start the both interfaces in the same time :
    mainWindow.show();
    secWindow,show();

    If i do in this way everything works ok.
    Here are same code samples:
    mainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include"secdialog.h"
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
        void openSecWindow();
    
    
    private:
        Ui::MainWindow *ui;
        secDialog* mySecDiag;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include"secdialog.h"
    #include <QApplication>
    #include<QString>
    #include<QFile>
    #include<QMessageBox>
    #include<QDebug>
    #include<QTextStream>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QObject::connect(ui->pushButton, SIGNAL(click()), this, SLOT(openSecWindow()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        this->close();
        //QProcess::execute("Benchmark\\Debug\\Benchmark.exe");
        secDialog dialog;
        dialog.setModal(true);
    
        dialog.exec();
    }
    
    void MainWindow::openSecWindow()
    {
        mySecDiag = new secDialog();
        mySecDiag->show();
    
        QFile file("E:/VS/QT/test2/data.txt");
        if(!file.open(QIODevice::ReadOnly)) {
            QMessageBox::information(0, "error", file.errorString());
        }
    
        QTextStream in(&file);
        QVector<QString> vector;
        while(!in.atEnd()) {
            QString line = in.readLine();
            vector.push_back(line);
            qDebug()<<line;
        }
    
        mySecDiag->setText("aici trebuie altceva");
        mySecDiag->setTextL2(vector[1]);
        mySecDiag->setTextL3(vector[1]);
        mySecDiag->lineEdit(vector[1]);
    
        file.close();
    }
    
    

    secDialog.h

    #ifndef SECDIALOG_H
    #define SECDIALOG_H
    
    #include <QDialog>
    
    namespace Ui {
    class secDialog;
    }
    
    class secDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit secDialog(QWidget *parent = 0);
        void setText  (const QString &text);
        void setTextL2(const QString &text);
        void setTextL3(const QString &text);
        void lineEdit (const QString &text);
        void textString(const QString &same_text);
        ~secDialog();
    
    private:
        Ui::secDialog *ui;
    };
    
    #endif // SECDIALOG_H
    
    

    secDialog.cpp

    #include "secdialog.h"
    #include "ui_secdialog.h"
    
    secDialog::secDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::secDialog)
    {
        ui->setupUi(this);
        ui->tabWidget->setCurrentIndex(0);
    }
    
    secDialog::~secDialog()
    {
        delete ui;
    }
    
    void secDialog::setText(const QString &text)
    {
        ui->label->setText(text);
    }
    
    void secDialog::setTextL2(const QString &text)
    {
        ui->label_2->setText(text);
    }
    
    void secDialog::setTextL3(const QString &text)
    {
        ui->label_3->setText(text);
    }
    
    void secDialog::lineEdit(const QString &text)
    {
        ui->lineEdit->setText(text);
    }
    
    

    And here also the main:

    #include "mainwindow.h"
    #include"secdialog.h"
    #include <QApplication>
    #include<QString>
    #include<QFile>
    #include<QMessageBox>
    #include<QDebug>
    #include<QTextStream>
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    

    How i have said above, if i make something like this in main:

    #include "mainwindow.h"
    #include"secdialog.h"
    #include <QApplication>
    #include<QString>
    #include<QFile>
    #include<QMessageBox>
    #include<QDebug>
    #include<QTextStream>
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        secDialog dialog;
        dialog.show();
    
        QFile file("E:/VS/QT/test2/data.txt");
        if(!file.open(QIODevice::ReadOnly)) {
            QMessageBox::information(0, "error", file.errorString());
        }
    
        QTextStream in(&file);
        QVector<QString> vector;
        while(!in.atEnd()) {
            QString line = in.readLine();
            vector.push_back(line);
            qDebug()<<line;
        }
    
        dialog.setText("aici trebuie altceva");
        dialog.setTextL2(vector[1]);
        dialog.setTextL3(vector[1]);
        dialog.lineEdit(vector[1]);
    
        file.close();
    
        return a.exec();
    }
    
    

    The labels are all populated. Can someone give me a clue about what is wrong?
    Thx :)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 10 Feb 2017, 18:13 last edited by mrjj 2 Oct 2017, 18:26
      #2

      Hi
      Where do you call MainWindow::openSecWindow() from ?
      Ahh ui->pushButton calls openSecWindow();

      You seems to create a new dialog here

      void MainWindow::on_pushButton_clicked()
      {
          this->close();
          //QProcess::execute("Benchmark\\Debug\\Benchmark.exe");
          secDialog dialog;
          dialog.setModal(true);
      
          dialog.exec();
      }
      

      That one will not get any items as no file read. calling openSecWindow() would.

      You could make a read function.

      void MainWindow::ReadLabels(secDialog* mySecDiag, QString fileName) {
        QFile file(fileName);
        if(!file.open(QIODevice::ReadOnly)) {
          QMessageBox::information(0, "error", file.errorString());
        }
      
        QTextStream in(&file);
        QVector<QString> vector;
        while(!in.atEnd()) {
          QString line = in.readLine();
          vector.push_back(line);
          qDebug() << line;
        }
      
        mySecDiag->setText("aici trebuie altceva");
        mySecDiag->setTextL2(vector[1]);
        mySecDiag->setTextL3(vector[1]);
        mySecDiag->lineEdit(vector[1]);
      
        file.close();
      }
      
      

      Are you aiming at one window at a time or many on same time?

      A 1 Reply Last reply 10 Feb 2017, 19:50
      0
      • A Offline
        A Offline
        Adrian.Aioanei
        wrote on 10 Feb 2017, 18:27 last edited by
        #3

        Yes, that's wirte.
        The new window pup up the old window is close.
        The problem is that in the secound window i have 3 labels that i am trying to populate them from a .txt file.

        #include "secdialog.h"
        #include "ui_secdialog.h"
        
        secDialog::secDialog(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::secDialog)
        {
            ui->setupUi(this);
            ui->tabWidget->setCurrentIndex(0);
        }
        
        secDialog::~secDialog()
        {
            delete ui;
        }
        
        void secDialog::setText(const QString &text)
        {
            ui->label->setText(text);
        }
        
        void secDialog::setTextL2(const QString &text)
        {
            ui->label_2->setText(text);
        }
        
        void secDialog::setTextL3(const QString &text)
        {
            ui->label_3->setText(text);
        }
        
        

        And this is not happening if the new window is pop up with the above function.
        If i start the both window from the main function everything works ok, but if the new window start when the button is pushed the labels aren't populated in the second window.

        1 Reply Last reply
        0
        • M mrjj
          10 Feb 2017, 18:13

          Hi
          Where do you call MainWindow::openSecWindow() from ?
          Ahh ui->pushButton calls openSecWindow();

          You seems to create a new dialog here

          void MainWindow::on_pushButton_clicked()
          {
              this->close();
              //QProcess::execute("Benchmark\\Debug\\Benchmark.exe");
              secDialog dialog;
              dialog.setModal(true);
          
              dialog.exec();
          }
          

          That one will not get any items as no file read. calling openSecWindow() would.

          You could make a read function.

          void MainWindow::ReadLabels(secDialog* mySecDiag, QString fileName) {
            QFile file(fileName);
            if(!file.open(QIODevice::ReadOnly)) {
              QMessageBox::information(0, "error", file.errorString());
            }
          
            QTextStream in(&file);
            QVector<QString> vector;
            while(!in.atEnd()) {
              QString line = in.readLine();
              vector.push_back(line);
              qDebug() << line;
            }
          
            mySecDiag->setText("aici trebuie altceva");
            mySecDiag->setTextL2(vector[1]);
            mySecDiag->setTextL3(vector[1]);
            mySecDiag->lineEdit(vector[1]);
          
            file.close();
          }
          
          

          Are you aiming at one window at a time or many on same time?

          A Offline
          A Offline
          Adrian.Aioanei
          wrote on 10 Feb 2017, 19:50 last edited by
          #4

          @mrjj I try to run one window a time.
          First just a simple window with a button and the second with same results .
          How should i use ReadLabels function in main?
          Something like this ?

           MainWindow w;
           secWindow* secWin;
           w.show();
           w.ReadLabels (secWin,"data.txt");
           return a.exec();
          

          This will not solve the problem because the new window doesn't pop up anymore.

          M 1 Reply Last reply 10 Feb 2017, 20:29
          0
          • A Adrian.Aioanei
            10 Feb 2017, 19:50

            @mrjj I try to run one window a time.
            First just a simple window with a button and the second with same results .
            How should i use ReadLabels function in main?
            Something like this ?

             MainWindow w;
             secWindow* secWin;
             w.show();
             w.ReadLabels (secWin,"data.txt");
             return a.exec();
            

            This will not solve the problem because the new window doesn't pop up anymore.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 10 Feb 2017, 20:29 last edited by
            #5

            @Adrian.Aioanei

            Something like

            void MainWindow::on_pushButton_clicked()
            {
            secDialog dialog;
            ReadLabels( &dialog, "E:/VS/QT/test2/data.txt");
            dialog.exec(); // if you use show() which is non blocking, you should use secDialog *dialog = new secDialog;
            }

            1 Reply Last reply
            3
            • A Offline
              A Offline
              Adrian.Aioanei
              wrote on 11 Feb 2017, 15:18 last edited by
              #6

              Yep, it works.
              Thanks :)

              1 Reply Last reply
              1

              5/6

              10 Feb 2017, 20:29

              • Login

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