如何从Qdialog更改QStackedWidget索引

How to change QStackedWidget index from Qdialog

本文关键字:QStackedWidget 索引 更改 Qdialog      更新时间:2023-10-16

我的应用程序在菜单栏中有一个"actionhelp",当点击打开一个QDialog,其中包含一个ok按钮在主窗口的另一边,我有一个QStackedWidget所以我的问题是如何改变堆叠小部件的索引,当我按下ok按钮在QDialog??

信号和槽位。将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);
    }
}

假设您在对话框中有一个行编辑,并且您希望根据行编辑值(或旋转框)更改堆叠小部件的索引:

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