Solved How to set switching screen condition?
-
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 ; } }
-
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 doint 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. -
@mrjj said in How to set swiching screen condition?:
QString Dialog::GetCheckStepStatus() {
return checkstep;
}Thank you mrjj's reply.
- My next screen will change to main window,it is correct.
- 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"; }
-
Hi and welcome to devnet,
Aren't you reimplementing QWizard ?
-
@RiceBall said in How to set swiching screen condition?:
QString Dialog::GetCheckStepStatus() {return checkstep;}
Hi
as a new function to the Dialog class. -
Sorry , I am not sure that is your meaning ??
-
@RiceBall Did you add GetCheckStepStatus() to your class declaration in the header file?
class Dialog { public: QString GetCheckStepStatus(); };
-
@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
-
Hi
include QDialog -
- 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();
?
-
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 -
@jsulm said in How to set switching screen condition?:
- 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; }