2013-03-19 46 views
1

我的应用程序在菜单栏中有一个“actionhelp”,点击后会打开一个包含ok窗口的QDialog,在主窗口的另一端,我有一个QStackedWidget 所以我的问题是当我按下QDialog中的确定按钮时,改变stackedwidget的索引?如何从Qdialog中更改QStackedWidget索引

+0

你有你的对话框上的东西,重要的是你?像一个QLineEdit? – saeed 2013-03-19 10:35:12

+0

没有只是标签中的某些“文本”,而不是从该QDialog发送到主窗口 – 2013-03-19 11:11:46

回答

2

信号和插槽。连接ok按钮的信号(或者在QDialog :: Accepted关闭后检测它自己发出的信号)到一个插槽,该插槽将更改QStackedWidget中的索引。

示例代码:

创建和主要方法连接的QAction:

QAction *displayDialog = new QAction("Display Dialog", this); 
connect(popup, SIGNAL(triggered()), this, SLOT(showDialog())); 

显示对话框:

void showDialog() 
{ 
    YourDialog *dialog = new YourDialog(this); 
    int return_code = dialog.exec(); 
    if (return_code == QDialog::Accepted) 
    { 
     int index = someValue; 
     qStackedWidget.setCurrentIndex(index); 
    } 
} 
+0

怎么办? – 2013-03-19 11:05:32

+0

为您添加了一些代码 – Ninjammer 2013-03-20 02:32:52

+0

这帮助了我对类似情况的疑问以及.. thanx .. – RicoRicochet 2015-07-23 09:12:57

0

假设你有你的对话框行编辑和你想改变基于行编辑值(或旋转框)的堆叠小部件的索引:

//your dialog 
//the constructor 
YourDialog::YourDialog(QWidget*parent) 
    :QDialog(parent) 
{ 
    connect(ur_ok_btn, SIGNAL(clicked()), SLOT(accept())); 
} 

//access to line edit value 
QString YourDialog::getUserEnteredValue(){return ur_line_edit->text();} 

在其中创建YourDialog类的一个实例:

//your main window 
    YourDialog dlg; 
    if(dlg.exec() == QDialog::Accepted){ 
     int i = dlg.getUserEnteredValue().toInt(); 
     ur_stacked_widget->setCurrentIndex(i); 
    } 
+0

试图给我这个结果:/home/sliver/Documents/Workspace/clinetCloud/mainwindow.cpp:34:error:can not调用构造函数'Logindialog :: Logindialog'直接[-fpermissive] – 2013-03-19 11:04:02

+0

也许你已经定义了你的构造函数私有。 – saeed 2013-03-19 14:18:29