如何使用 Qt C++ 在 QDialog Window 和 QMainWindow 之间进行通信

How to communicate between QDialog Window and QMainWindow with Qt C++

我是一个在C++中使用Qt Creator的应用程序。 int main()函数如下。 我正在启动一个 QDialog Window 并在其中获取 "url" 变量内容。

如何将此变量放入 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
}

为 qt 应用程序生成的标准主文件包含以下行:

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

    return a.exec();
}

主要 window 是在那里创建的。

因此,在您的情况下,您可以在 return 之前创建主 window。 通过在构造函数中添加 QString 作为参数,url 将在 mainwindow.

中可用

所以 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();
}

您应该正确了解信号和槽,以便在对象之间传递变量。文档在这里:http://doc.qt.io/qt-4.8/signalsandslots.html

另一种方法是在 MainWindow 中执行 QDialog,这将是最佳实践。在有用的范围内使用控件。您不必在主函数中使用对话框。

所以,看看这两种解决方案。对于简单变量,最好的方法是在将使用值的 class 内执行对话,第二个是如果必须在复杂的 class 之间传递值,则使用信号和槽。

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");
}

如果你只想输入字符串,我建议你看看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);
}