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 set switching screen condition?

How to set switching screen condition?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.0k 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.
  • R Offline
    R Offline
    RiceBall
    wrote on last edited by aha_1980
    #1

    I'm a new hand.
    I follow some training video to make program.
    Now I make a dialog button to select folder it need to include 2 files(apps-general.xml / settings-general.xml)
    And Next button need to check does it comfort condition.
    If it is correct,it can switch to another screen.
    If wrong,it can't switch screen.

    main.cpp

    //
    #include "dialog.h"
    #include <QApplication>
    #include "mainwindow.h"
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        MainWindow m;
    
        if(w.exec() == QDialog::Accepted)
        {
            m.show();
            return a.exec();
        }
        else return 0;
    }
    

    dialog.h

    //
    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QFileInfo>
    #include <QTextBrowser>
    #include <QVBoxLayout>
    #include <QString>
    #include <QMessageBox>
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = nullptr);
        ~Dialog();
        void showFile();                            //宣告"開啟對話視窗選擇資料夾位置"函數
    
        QString file, file_name, file_path;
        QFileInfo fi;
        QTextBrowser *textBrowser_ANPRResult;
        QString str1,str2;
        QString checkstep;
        QMessageBox msg;
    
    
    private:
        Ui::Dialog *ui;
    
    private slots:                              //宣告訊號溝槽位置
    
        void on_pushButton_3_clicked();
        void on_pushButton_2_clicked();
        void on_pushButton_clicked();
    };
    
    #endif // DIALOG_H
    
    

    dialog.cpp

    //#include "dialog.h"
    #include "ui_dialog.h"
    
    #include <QFileDialog>
    #include <QStringList>
    #include <QDialog>
    #include <QMessageBox>
    #include <QFileInfo>
    #include <QString>
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    
    void Dialog::showFile()                                                 //定義showFile函數功能(產生對話視窗,選擇資料夾)
    {
    
        QFileDialog* fileDialog = new QFileDialog(this);                        //設定檔案對話視窗
    
            fileDialog->setWindowTitle("選取iHMI資料夾");                        //設定標題
            //fd->setDirectory(buf);
            fileDialog->setFileMode( QFileDialog::DirectoryOnly );              //設定僅能選取資料夾格式
            QStringList fileName;                                               //定義字串陣列名稱 fileName
    
            if ( fileDialog->exec() == QDialog::Accepted )                      //判斷如果點選確認後的判斷式
            {
                fileName = fileDialog->selectedFiles();                         //定義擷取的fileDialog資料到fileName
                //srcDir.setPath(fileName.at(0));
                ui->textBrowser->append(fileName.join(","));                    //將fileName送到textBrowser視窗顯示,並每個物件都透過(,)分隔
            }
            else
            {
                return;
            }
    
    
            QString dir1 = fileName.join(",");   //將fileName 從QStringList轉換成QString 文字
            QString dir2 = fileName.join(",");   //將fileName 從QStringList轉換成QString 文字
            QString check1 = "/apps-general.xml";
            QString check2 = "/settings-general.xml";
             str1= dir1.append(check1);         //將路徑與固定檔案結合
             str2= dir2.append(check2);         //將路徑與固定檔案結合
    
             if(QFileInfo(str1).exists() && QFileInfo(str2).exists()){              //指定文件確認是否有找到
                 msg.setText("指定位置正確,開始進行""下一步""設定");
                 msg.exec();
                 checkstep = "OK";
             }
             else{
                 msg.setText("apps-general.xml或\n"
                             "settings-general.xml\n"
                             "檔案沒找到,請重新指向路徑");
                 msg.exec();
                 checkstep = "NG";
             }
    
    }
    
    void Dialog::on_pushButton_3_clicked()
    {
        showFile();
    }
    
    
    void Dialog::on_pushButton_2_clicked()
    {
        close();
    }
    
    void Dialog::on_pushButton_clicked()
    {
    
        if (checkstep == "OK")
        {}
        else {
            return ;
        }
    
    }
    
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums.
      What would next screen be ?
      The main window ?

      You could add
      QString Dialog::GetCheckStepStatus() {
      return checkstep;
      }
      and then in main do

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          Dialog w;
          MainWindow m;
         QDialog::DialogCode res = w.exec();
          if(res  == QDialog::Accepted && w.GetCheckStepStatus() == "OK" )
          {
              m.show();
              return a.exec();
          }
          else return 0;
      }
      

      Note, instead of QString, checkstep could just be a bool to return true/false.

      Also, it will still qui the application on cancel or if check is not good.
      If you want to ask for folder again, you could use a while loop.

      R 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi and welcome to the forums.
        What would next screen be ?
        The main window ?

        You could add
        QString Dialog::GetCheckStepStatus() {
        return checkstep;
        }
        and then in main do

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            Dialog w;
            MainWindow m;
           QDialog::DialogCode res = w.exec();
            if(res  == QDialog::Accepted && w.GetCheckStepStatus() == "OK" )
            {
                m.show();
                return a.exec();
            }
            else return 0;
        }
        

        Note, instead of QString, checkstep could just be a bool to return true/false.

        Also, it will still qui the application on cancel or if check is not good.
        If you want to ask for folder again, you could use a while loop.

        R Offline
        R Offline
        RiceBall
        wrote on last edited by
        #3

        @mrjj said in How to set swiching screen condition?:

        QString Dialog::GetCheckStepStatus() {
        return checkstep;
        }

        Thank you mrjj's reply.

        1. My next screen will change to main window,it is correct.
        2. QString Dialog::GetCheckStepStatus() {return checkstep;}
          Where should I add ?

        Add to this part ?? Or ??

        //
        if(QFileInfo(str1).exists() && QFileInfo(str2).exists()){              //指定文件確認是否有找到
                     msg.setText("指定位置正確,開始進行""下一步""設定");
                     msg.exec();
                     checkstep = "OK";
                 }
                 else{
                     msg.setText("apps-general.xml或\n"
                                 "settings-general.xml\n"
                                 "檔案沒找到,請重新指向路徑");
                     msg.exec();
                     checkstep = "NG";
                 }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          Aren't you reimplementing QWizard ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @RiceBall said in How to set swiching screen condition?:

            QString Dialog::GetCheckStepStatus() {return checkstep;}

            Hi
            as a new function to the Dialog class.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              RiceBall
              wrote on last edited by
              #6

              0_1556169834494_52d2bb79-56c0-4370-b30e-65f6f939b320-image.png

              Sorry , I am not sure that is your meaning ??

              jsulmJ 1 Reply Last reply
              0
              • R RiceBall

                0_1556169834494_52d2bb79-56c0-4370-b30e-65f6f939b320-image.png

                Sorry , I am not sure that is your meaning ??

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @RiceBall Did you add GetCheckStepStatus() to your class declaration in the header file?

                class Dialog
                {
                public:
                    QString GetCheckStepStatus();
                };
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  RiceBall
                  wrote on last edited by
                  #8

                  @mrjj said in How to set switching screen condition?:

                  QDialog::DialogCode res = w.exec();
                  if(res == QDialog::Accepted && w.GetCheckStepStatus() == "OK" )
                  {
                  m.show();
                  return a.exec();
                  }

                  I have edit in this ,but now I show another alarm

                  0_1556185150720_6f680fde-50c2-4ec9-8547-73c35c2daa80-image.png

                  jsulmJ 1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi
                    include QDialog

                    1 Reply Last reply
                    0
                    • R RiceBall

                      @mrjj said in How to set switching screen condition?:

                      QDialog::DialogCode res = w.exec();
                      if(res == QDialog::Accepted && w.GetCheckStepStatus() == "OK" )
                      {
                      m.show();
                      return a.exec();
                      }

                      I have edit in this ,but now I show another alarm

                      0_1556185150720_6f680fde-50c2-4ec9-8547-73c35c2daa80-image.png

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @RiceBall

                      1. DialogCode is a member variable of Dialog, so
                      DialogCode res = w.exec();
                      

                      is simply invalid C++. What do you actually want to do in that line?
                      2. DialogCode is a QString and you're trying to assign an int to it. Again invalid C++.

                      Why not simply

                      int res = w.exec();
                      

                      ?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      R 1 Reply Last reply
                      1
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi
                        It's actually an enum.
                        enum DialogCode { Rejected, Accepted };
                        but yes int will work just as fine as long they dont use enum class.
                        Besides exec just returns int.
                        My fault. i showed QDialog::DialogCode

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @RiceBall

                          1. DialogCode is a member variable of Dialog, so
                          DialogCode res = w.exec();
                          

                          is simply invalid C++. What do you actually want to do in that line?
                          2. DialogCode is a QString and you're trying to assign an int to it. Again invalid C++.

                          Why not simply

                          int res = w.exec();
                          

                          ?

                          R Offline
                          R Offline
                          RiceBall
                          wrote on last edited by
                          #12

                          @jsulm said in How to set switching screen condition?:

                          @RiceBall

                          1. DialogCode is a member variable of Dialog, so
                          DialogCode res = w.exec();
                          

                          is simply invalid C++. What do you actually want to do in that line?
                          2. DialogCode is a QString and you're trying to assign an int to it. Again invalid C++.

                          Why not simply

                          int res = w.exec();
                          

                          ?

                          It's work, Thank you for your reply.

                          My final code like this

                          dialog.h

                          //
                          #ifndef DIALOG_H
                          #define DIALOG_H
                          
                          #include <QDialog>
                          #include <QFileInfo>
                          #include <QTextBrowser>
                          #include <QVBoxLayout>
                          #include <QString>
                          #include <QMessageBox>
                          
                          
                          namespace Ui {
                          class Dialog;
                          }
                          
                          class Dialog : public QDialog
                          {
                              Q_OBJECT
                          
                          public:
                              explicit Dialog(QWidget *parent = nullptr);
                              ~Dialog();
                              void showFile();                            //宣告"開啟對話視窗選擇資料夾位置"函數
                          
                              QString file, file_name, file_path;
                              QFileInfo fi;
                              QTextBrowser *textBrowser_ANPRResult;
                              QString str1,str2;
                              QString checkstep;
                              QMessageBox msg;
                          
                              QString GetCheckStepStatus();
                              QString DialogCode;
                          
                          
                          
                          
                          
                          private:
                              Ui::Dialog *ui;
                          
                          
                          
                          
                          
                          
                          private slots:                              //宣告訊號溝槽位置
                          
                              void on_pushButton_3_clicked();
                              void on_pushButton_2_clicked();
                              void on_pushButton_clicked();
                          };
                          
                          #endif // DIALOG_H
                          
                          

                          dialog.cpp

                          #include "dialog.h"
                          #include "ui_dialog.h"
                          
                          #include <QFileDialog>
                          #include <QStringList>
                          #include <QDialog>
                          #include <QMessageBox>
                          #include <QFileInfo>
                          #include <QString>
                          
                          Dialog::Dialog(QWidget *parent) :
                              QDialog(parent),
                              ui(new Ui::Dialog)
                          {
                              ui->setupUi(this);
                          
                          }
                          
                          Dialog::~Dialog()
                          {
                              delete ui;
                          }
                          
                          
                          QString Dialog::GetCheckStepStatus() {
                          return checkstep;
                          }
                          
                          void Dialog::showFile()                                                 //定義showFile函數功能(產生對話視窗,選擇資料夾)
                          {
                          
                              QFileDialog* fileDialog = new QFileDialog(this);                        //設定檔案對話視窗
                          
                                  fileDialog->setWindowTitle("選取iHMI資料夾");                        //設定標題
                                  //fd->setDirectory(buf);
                                  fileDialog->setFileMode( QFileDialog::DirectoryOnly );              //設定僅能選取資料夾格式
                                  QStringList fileName;                                               //定義字串陣列名稱 fileName
                          
                                  if ( fileDialog->exec() == QDialog::Accepted )                      //判斷如果點選確認後的判斷式
                                  {
                                      fileName = fileDialog->selectedFiles();                         //定義擷取的fileDialog資料到fileName
                                      //srcDir.setPath(fileName.at(0));
                                      ui->textBrowser->append(fileName.join(","));                    //將fileName送到textBrowser視窗顯示,並每個物件都透過(,)分隔
                                  }
                                  else
                                  {
                                      return;
                                  }
                          
                          
                                  QString dir1 = fileName.join(",");   //將fileName 從QStringList轉換成QString 文字
                                  QString dir2 = fileName.join(",");   //將fileName 從QStringList轉換成QString 文字
                                  QString check1 = "/apps-general.xml";
                                  QString check2 = "/settings-general.xml";
                                   str1= dir1.append(check1);         //將路徑與固定檔案結合
                                   str2= dir2.append(check2);         //將路徑與固定檔案結合
                          
                          
                          
                          
                          
                                   if(QFileInfo(str1).exists() && QFileInfo(str2).exists()){              //指定文件確認是否有找到
                                       msg.setText("指定位置正確,開始進行""下一步""設定");
                                       msg.exec();
                                       checkstep = "OK";
                                   }
                                   else{
                                       msg.setText("apps-general.xml或\n"
                                                   "settings-general.xml\n"
                                                   "檔案沒找到,請重新指向路徑");
                                       msg.exec();
                                       checkstep = "NG";
                                   }
                          
                          }
                          
                          void Dialog::on_pushButton_3_clicked()
                          {
                              showFile();
                          }
                          
                          
                          void Dialog::on_pushButton_2_clicked()
                          {
                              close();
                          }
                          
                          void Dialog::on_pushButton_clicked()
                          {
                          }
                          
                          

                          main.cpp

                          #include "dialog.h"
                          #include <QApplication>
                          #include "mainwindow.h"
                          
                          
                          int main(int argc, char *argv[])
                          {
                              QApplication a(argc, argv);
                              Dialog w;
                              MainWindow m;
                              int res = w.exec();
                          
                               if(res  == QDialog::Accepted && w.GetCheckStepStatus() == "OK" )
                               {
                                   m.show();
                                   return a.exec();
                               }
                              else return 0;
                          }
                          
                          

                          0_1556204386541_final code.gif

                          1 Reply Last reply
                          1

                          • Login

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