2016-12-14 78 views
1

我是一個在C++中使用Qt Creator的應用程序。 int main()函數如下。 我開始一個QDialog窗口,我正在獲取「url」變量內容。如何使用Qt C++在QDialog窗口和QMainWindow之間進行通信

如何將此變量放入mainwindow.cpp文件中。

QApplication a(argc, argv); 
MainWindow *w; //Put it here in case you have several cases where you want to initialize it 

QLoginDialog d; //Your login dialog 
if(d.exec()==QDialog::Accepted) 
{ 
    if(d.isLogged()) //Check the variable used to validate the login 
    { 
     w = new MainWindow; 
     w->show(); 
    } 
    else 
    { 
     //Handle bad login 
     return -1; //Don't forget to return something here 
    } 
} 
else 
{ 
    //Handle dialog not validated 
    return -1; //Don't forget to return something here 
} 

return a.exec(); 

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent): 
     QMainWindow(parent), 
     ui(new Uİ::MainWindow) 
{ 
    QDialog s; 
    s.show(); 
    s.setmodel(true); 

    if(s.exec()==QDialog::Accepted) 
    { 
    if(s.URL=="true") 
    { 
     ui.setupUi(this); 
    } 
    } 
} 

QDialog.cpp = Start.cpp源代碼:

#include "start.h" 
#include "ui_start.h" 

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

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

void Start::on_pushButton_2_clicked() 
{ 
    QString dirName=QFileDialog::getExistingDirectory(
     this, 
     tr("Select a Directory"), 
     QDir::currentPath()); 

    if(!dirName.isNull()) 
     ui->lineEdit_2->setText(dirName); 
} 

void Start::on_pushButton_3_clicked() 
{ 
    URL=ui->lineEdit->text(); 
    Directory=ui->lineEdit_2->text(); 

    if(URL.isEmpty() && Directory.isEmpty()) 
    { 
     QMessageBox::warning(this,tr("Error"),tr("Please, fill in the blanks")); 
    } 
    else if(URL.isEmpty()) 
    { 
     QMessageBox::warning(this,tr("Error"),tr("is not a valid URL or Path!!")); 
    } 
    else if(Directory.isEmpty()) 
    { 
     QMessageBox::warning(this,tr("Error"),tr("is not a select directory!!")); 
    } 



    ControlConfiguration(); 
} 

void Start::ControlConfiguration() 
{ 
    QString configPosition= ui->lineEdit_2->text(); 

    QString str1="CHECKOUT_HOME"; 
    QString str2="new_checkout.csh"; 
    QString str3="setup_prjsvn.csh"; 

    QMessageBox *msgBox=new QMessageBox(this); 
    int sayac=0; 
    QDir path(configPosition); 

    QStringList files=path.entryList(QDir::Files); 
    for(int i=0; i<files.length(); i++) 
    { 
     if(files[i].compare(str1)==0) 
     { 
      sayac=sayac+1; 
     } 
    } 
    for(int i=0; i<files.length(); i++) 
    { 
     if(files[i].compare(str2)==0) 
     { 
      sayac=sayac+1; 
     } 
    } 
    for(int i=0; i<files.length(); i++) 
    { 
     if(files[i].compare(str3)==0) 
     { 
      sayac=sayac+1; 
     } 
    } 

    if(sayac>=3)//Attention Flurent. if true, open QMainWindow 
    { 
     QMessageBox::information(this,tr(" "),tr("Configuration Directory")); 
     //s_path=ui->lineEdit->text(); 

     this->hide(); 

     s_path="true"; 

     URL=ui->lineEdit_2->text(); 
    } 
    else 
    { 
     msgBox->setInformativeText("Would you like to configure?"); 
     msgBox->setStandardButtons(QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes); 
     msgBox->setDefaultButton(QMessageBox::Yes); 
     int ret = msgBox->exec(); 
     switch(ret){ 
     case QMessageBox::Yes: 
      Configuration(); 
      break; 
     case QMessageBox::No: 
      //ui->locationEdit->clear(); 
      break; 
     case QMessageBox::Cancel: 
      break; 

     } 

    } 
} 
void Start::Configuration() 
{ 
    QString projePosition= ui->lineEdit_2->text(); // Proje konumu alınır. 
    QProcess* process=new QProcess(this); 
    QMessageBox *msgBox=new QMessageBox(this); 

    process->setWorkingDirectory(projePosition); 

    QString svn_export="svn export  http://atlas/svn/prjsvn/trunk/common/common_bin/istanbul/new_checkout.csh"; 
    QString s_newcheckout="source new_checkout.csh"; 
    QString s_prjsvn="source setup_prjsvn.csh"; 

    process->start("tcsh -c \""+svn_export+";"+s_newcheckout+";"+s_prjsvn+"\""); 

    process->waitForBytesWritten(); 
    process->waitForFi 
} 

回答

2

的標準生成的主文件Qt應用程序包含以下行:

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

    return a.exec(); 
} 

主窗口我在那裏創建。

所以在你的情況下,你可以在你返回之前創建主窗口。 通過在構造函數中添加QString作爲參數,URL將在主窗口中可用。

所以mainWindow.cpp構造變化:

MainWindow::MainWindow(QString url, QWidget *parent) : 
    QMainWindow(parent), ui(new Uİ::MainWindow) 
{ 
    ui.setupUi(this); 

    //Place your code here that uses the url, 
} 

所以mainWindow.h構造變化:

MainWindow::MainWindow(QString url = "", QWidget *parent = 0); 

和main.cpp中的變化:

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    QString url; //Fill this from your dialog. 
    MainWindow w(url);//Pass the url to the main window 
    w.show(); 

    return a.exec(); 
} 
+0

更好的做法是執行MainWindow類中的對話框。它有助於保持代碼的組織性,因爲該值應該在MainWindow中使用。 –

+0

謝謝,我曾經工作源代碼@TomConijin –

2

您應該正確看待信號和插槽,以在對象之間傳遞變量。文檔在這裏:http://doc.qt.io/qt-4.8/signalsandslots.html

另一種方法是在你的MainWindow中執行QDialog,這將是最好的做法。在他們有用的範圍內使用控件。你不應該在主函數中使用對話框。

所以,看看這兩種解決方案。對於簡單變量來說,最好的方法是在類中使用值來執行對話框,其次是在複雜類之間傳遞值時使用信號和槽。

的main.cpp

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow *w; //Put it here in case you have several cases where you want to initialize it 

    QLoginDialog d; //Your login dialog 
    if(d.exec()==QDialog::Accepted) 
    { 
     if(d.isLogged()) //Check the variable used to validate the login 
     { 
      w = new MainWindow(d.getUrl()); //I guess the URL comes from your login dialog, so make a function that returns it 
      w->show(); 
     } 
     else 
     { 
      //Handle bad login 
      return -1; //Don't forget to return something here 
     } 
    } 
    else 
    { 
     //Handle dialog not validated 
     return -1; //Don't forget to return something here 
    } 

    return a.exec(); 
} 

mainwindow.cpp

//Don't forget to add the "QString sUrl" to your mainwindow.h 
MainWindow::MainWindow(QString sUrl, QWidget *parent): QMainWindow(parent), ui(new Uİ::MainWindow) 
{ 
    ui.setupUi(this); 

    qDebug()<<s.URL; //It will be displayed here 
} 

你dialog.cpp

//Use this to return the boolean value that represents if the path is valid or not. 
bool QDialog::isLogged() 
{ 
    return this->s_path.equals("true"); 
} 
+0

謝謝,但我想先啓動QDialogWindow。因爲ıt是登錄面板。後來第二次啓動QDialogwindow裏面的QMainwindow @FlorentUguet –

+0

Qdialog.cpp ----舉例:if(count> 3){QMainwindow * p = new QMainWindow(); P->的setAttribute(Qt的:: WA_DeleteOnClose); p-> show} --- @ FlorentUguet –

+0

切勿從對話框中啓動窗口小部件或窗口。從對話框中返回一個值,然後處理你想要的。對話框是臨時的小部件,不應該是窗口/小部件的父母。 –

0

如果你想要做的就是要求一個輸入字符串,我建議你看看QInputDialog

示例:

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    bool ok; 
    QString message = QInputDialog::getText 
    (0, "Input","URL:", QLineEdit::Normal,"", &ok); 
    qDebug()<<message; 
    ui->setupUi(this); 
} 
+0

什麼是這個boolflag?我想首先啓動Qdialogwindow,啓動時應用程序@me_alok –

+0

&boolflag是對布爾標誌變量的引用,它在編輯中被替換爲bool ok。 –

+0

當應用程序啓動時,對話框將出現 –

相關問題