How to synchronize QWidgets with menu bar?
I a开发者_Go百科m new to Qt creator. I have a stacked widget with 3 pages. I also have a menu bar that contains : open \\ create
. The QWidget contains 2 pages. I would like to ask how can I synchronize Open with first page and create from menu bar with second page from stacked widget?
I did write: ui->stackedWidget->show();
but it's printing the second page to both open and create.
Need some help.
You have to declare two slots in your MainWindow class. For instance:
class MainWindow : public QMainWindow
{
...
public slots:
void slotOpen() ;
void slotCreate() ;
...
} ;
Then, in your MainWindow constructor (assuming your menu actions are actionOpen
and actionCreate
):
connect (ui -> actionOpen, SIGNAL(triggered()), SLOT(slotOpen())) ;
connect (ui -> actionCreate, SIGNAL(triggered()), SLOT(slotCreate())) ;
The slot functions:
void MainWindow::slotOpen()
{
ui -> stackedWidget -> setCurrentIndex(0) ;
}
void MainWindow::slotCreate()
{
ui -> stackedWidget -> setCurrentIndex(1) ;
}
You can connect the menu actions to the QStackedWidget
Slot setCurrentIndex
. This will allow you to display the correct widget when clicking on the menu.
精彩评论