我在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) ;
mainwindow.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,我也决定将数据保存在标签中。在跟踪情绪中,我看到字符串是发送但当对话框显示标签变为默认值时。
我找不到错误。你能帮帮我吗?
答案 0 :(得分: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();