PDA

View Full Version : About mainwindows layout problem(segmentation fault)



kenchan
6th December 2012, 13:17
i want to implement below function:

mainwindow have two widget(A is stackwidget and B is QFrame).

1. at first A is showed.
2. when click QAction action_B, A is hided, B is showed.
3. when click QAction action_A, A is showed, B is hided.

when debugger.
At first click action_A, can show A and hide B, but segmentation fault when i cllick action_B.

i delete this->setCentralWidget(A) or this->setCentralWidget(B) , no fault was happened.

if i want to implement function above what i said. how to program? please give me any advice. thank your for your help!


codes see as below:


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

...............
this->setCentralWidget(A);
}

void MainWindow::init();
{
ui->action_A->setEnabled(true);
ui->action_B->setEnabled(false);

a->hide();
..........
this->setCentralWidget(B);
b->show();
}

void MainWindow::on_action_A_triggered()
{
b->hide();
a->show();
ui->action_A->setEnabled(false);
ui->action_B->setEnabled(true);
......
}

void MainWindow::on_action_B_triggered()
{
init();
}



TKS.

ken

anda_skoa
6th December 2012, 20:14
It looks like you want both of them to be in the central widget role of the main window.

Why don't you just create a stack widget and put both A and B into it and then set the new stack widget as the central widget?

Cheers,
_

amleto
6th December 2012, 20:21
Why refuse to use code tags?

Please respond to my latest post here http://www.qtcentre.org/threads/52310-Segment-fault


mainwindow have two widget(A is stackwidget and B is QFrame).
No it doesn't.




MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

...............
this->setCentralWidget(A);
}

Read my sig.

kenchan
7th December 2012, 02:35
Hi anda_skoa,

create new stack widget can be achieved.

but anther problem, several widgets into A. if i want the widget to be fullscreen that maybe have trouble.


by the way, i have two method,but have some problem. please give me any advice.
1. i use function as below, but info 'd_func() is private etc' when debugged.


void QMainWindow::setCentralWidget(QWidget *widget)
{
Q_D(QMainWindow);
if (d->layout->centralWidget() && d->layout->centralWidget() != widget) {
d->layout->centralWidget()->hide();
d->layout->centralWidget()->deleteLater();
}
d->layout->setCentralWidget(widget);
}

2. use layout , and then use show and hide function to set the widget into central widget, but segmentation fault also.

QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(A);
layout->addWidget(B);
this->centralWidget()->setLayout(layout);

Added after 46 minutes:

Hi All,

i have handled the problem.

Thank your for your advice.

Best Regard.

ken

amleto
7th December 2012, 08:21
If there is an existing central widget, and you call setcentralwidget again, the previous one will be deleted! Your pointer will be invalid.

Also,

CODE TAGS