Pop up a new window on a pushed button



  • 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 :)


  • Qt Champions 2016

    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?



  • 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.



  • @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.


  • Qt Champions 2016

    @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;
    }



  • Yep, it works.
    Thanks :)


Log in to reply
 

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