【问题标题】:Why my data can not transfer between forms with SIGNAl/SLOT?为什么我的数据不能在带有 SIGNAl/SLOT 的表格之间传输?
【发布时间】:2016-06-18 04:45:58
【问题描述】:

我在 Qt 中有主窗口和 1 个对话框(在 Linux 操作系统中)。我想从主窗口发送一些东西到我的对话框。当用户按下菜单按钮时,单击我的按钮会发出信号。这是我在 main.cpp 中的代码:

MainWindow w;
MyDialog m;
//------------------------------
//this connection send key button press mood from MainWindow
QObject::connect(&w,SIGNAL(pressMood(QString)),
          &m,SLOT(getPressMood(QString)));
w.show();  

这是我的 mainwindos.h:

signals:
    void pressMood(QString mood) ;

主窗口.cpp:

void MainWindow::on_btnMenu_clicked()
{
    if(database->checkEmpty())
    {
        menu mn;/*=new menu();*/
        mn.showFullScreen();
    }
    else
    {
       MyDialog *d=new MyDialog(this);

       d->show();
       d->raise();
       d->activateWindow();
emit pressMood("menu");
       if(d->Accepted>0)
       {
           if(loginResult)
           {
               menu *mn=new menu();
               mn->showFullScreen();
           }
       }
       else
           QMessageBox::warning(this, tr("Login failed"), "Sorry.Your authenticate is not valid.", QMessageBox::Ok);
    }
}
//--------------------------------------------
void MainWindow::on_btnPassword_clicked()
{
    //emit sendID2(result);
    CardDialog *d=new CardDialog(this);
     emit pressMood("pass");
    d->show();
    d->raise();

    if(d->Accepted<=0)
        QMessageBox::warning(this, tr("Login failed"), "Sorry.Your authenticate is not valid.", QMessageBox::Ok);

}

我不使用dialog.exec(),因为我不需要显示模态。
MyDialog.h:

public slots:
    void getPressMood(QString mood);

和 MyDialog.cpp: //================================================= =

void MyDialog::getPressMood(QString mood)
{
    mood=mood;
   //ui->lblMood->setText(mood);;
   //ui->lblMood->hide();
}
void MyDialog::on_buttonBox_accepted()
{
    //QString mood=ui->lblMood->text();
    bool st=database->checkPassword(ui->txtID->text(),ui->txtPass->text(),"3");
    int id=(ui->txtID->text()).toInt();
    //this user is valid to go to menu page
    //s/he is admin
    if((st)&&
            mood=="menu" &&
            database->checkAdmin(id))
    {
         .......
    }

当我逐行跟踪我的代码时。发射信号有效,它以另一种形式将字符串数据发送到我的插槽,getpressedmood() 插槽也有效。但是当对话框显示时全局 var 情绪变为 NULL,我也决定将数据保存在标签中。在跟踪心情中,我看到字符串已发送,但当对话框显示标签变为默认值时。 我找不到错误。你能帮帮我吗?

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    解决了。
    我的错误是在 main.cpp 中连接信号和插槽。答案是:

        MyDialog *d=new MyDialog(this);
    //should connect here not in main.cpp
               QObject::connect(this,SIGNAL(pressMood(QString)),
                              d,SLOT(getPressMood(QString)));
               emit pressMood("menu");
               d->show();
               d->raise();
               d->activateWindow();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多