PDA

View Full Version : How to change QStackedWidget index from Qdialog



sliverTwist
19th March 2013, 11:26
My application have an "actionhelp" in the menu bar which when clicked opens up a QDialog that contains an ok button and some text in a label.
At the other side in the mainwindow i have a QStackedWidget.
So my question is how to change the index of the stackedwidget when i press that ok button in the QDialog?? :confused:

Added after 4 minutes:

I have to mention that i found this code

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

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


//your main window
YourDialog dlg;
if( dlg.exec() == QDialog::Accepted ){
int i = dlg.getUserEnteredValue().toInt();
ur_stacked_widget->setCurrentIndex(i);
}
But it didn't helped me too much because i get mainly this error

error: cannot call constructor 'YourDialog::YourDialog ' directly [-fpermissive]

karankumar1609
19th March 2013, 11:30
Try this, if this will help you


//your dialog
//the constructor
YourDialog::YourDialog()
{
connect(ur_ok_btn, SIGNAL(clicked()), this, SLOT(accept ()));
}

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


//your main window
YourDialog *dlg = new YourDialog();
if( dlg->exec()){
qDebug() << " returned index number : " << dlg->getUserEnteredValue();
int i = dlg->getUserEnteredValue().toInt();
ur_stacked_widget->setCurrentIndex(i);
}



check if getUserEnteredValue return a correct index number.

CHEERS

sliverTwist
19th March 2013, 11:38
Sorry sir that didn't helped me too much
I have did this



//My dialog
Logindialog::Logindialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Logindialog)
{
ui->setupUi(this);
connect(ui->configurePushButton,SIGNAL(clicked()), this, SLOT(accept ()));
}

I have an action on the button that closes the dialog if clicked


void Logindialog::on_configurePushButton_clicked()
{
close();
}

and this is the creation of the Dialog

Logindialog nLD;
nLD.setModal(true);
nLD.setWindowFlags( Qt::Dialog | Qt::Desktop
|Qt::CustomizeWindowHint |Qt::CustomizeWindowHint
| Qt::WindowTitleHint| Qt::WindowMinMaxButtonsHint |
Qt::WindowMinimizeButtonHint );
nLD.exec();
if( nLD.exec() ){
ui->stackedWidget->setCurrentIndex(0);
}

karankumar1609
19th March 2013, 12:05
closing a dialog will not catch by if condition.

change

void Logindialog::on_configurePushButton_clicked()
{
close();
}
to

void Logindialog::on_configurePushButton_clicked()
{
accept();
}

d_stranz
20th March 2013, 15:32
But it didn't helped me too much because i get mainly this error
error: cannot call constructor 'YourDialog::YourDialog ' directly [-fpermissive]


Maybe you have solved your problem by now. Karankumar1609's suggestion in the first reply is not correct - if you can't construct a YourDialog instance on the stack, you won't be able to construct it with new() either.

What does the declaration of the YourDialog look like in YourDialog.h? Is YourDialog derived from QDialog? Does it declare Q_OBJECT? Does the constructor for YourDialog have a QWidget * argument (for the parent)? Your cpp code is not correct if that is how you actually implemented the constructor for your dialog class.

anda_skoa
20th March 2013, 19:24
Sorry sir that didn't helped me too much
I have did this



//My dialog
Logindialog::Logindialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Logindialog)
{
ui->setupUi(this);
connect(ui->configurePushButton,SIGNAL(clicked()), this, SLOT(accept ()));
}

I have an action on the button that closes the dialog if clicked


void Logindialog::on_configurePushButton_clicked()
{
close();
}

You don't need that you already have the button connected to the accept() slot.
If you need to do anything additional to accepting (this closing the dialog and making exec() return QDIalog::Accepted), simply overwrite accept() (it is a virtual method for exactly that reason)

Cheers,
_