可以使用Item Widgets组件QTreeWidget或QListWidget作为子窗体的导航菜单,QTabWidget做子窗体区域显示,达到主界面区域导航的效果。
//设置导航菜单
void HelloQt::SetNavigationTree()
{
QTreeWidgetItem* layoutsItem = new QTreeWidgetItem(ui.navigationTree);
layoutsItem->setText(0, tr("Layouts"));
QTreeWidgetItem* layouts_vertical_layout = new QTreeWidgetItem(layoutsItem);
layouts_vertical_layout->setText(0, tr("Vertical Layout"));
QTreeWidgetItem* layouts_horizontal_layout = new QTreeWidgetItem(layoutsItem);
layouts_horizontal_layout->setText(0, tr("Horizontal Layout"));
QTreeWidgetItem* layouts_grid_layout = new QTreeWidgetItem(layoutsItem);
layouts_grid_layout->setText(0, tr("Grid Layout"));
QTreeWidgetItem* layouts_form_layout = new QTreeWidgetItem(layoutsItem);
layouts_form_layout->setText(0, tr("Form Layout"));
}
//导航到子窗体
void HelloQt::NavigationToView(QTreeWidgetItem* item, int column)
{
if (item->text(column) == tr("Vertical Layout"))
{
if (qtreewidgettestui == nullptr)
{
qtreewidgettestui = new QTreeWidgetTestUI(ui.tabWidget);
ui.tabWidget->addTab(qtreewidgettestui, tr("Vertical Layout"));
}
ui.tabWidget->setCurrentWidget(qtreewidgettestui);
}
else if (item->text(column) == tr("Horizontal Layout"))
{
if (qlistwidgettestui == nullptr)
{
qlistwidgettestui = new QListWidgetTestUI(ui.tabWidget);
ui.tabWidget->addTab(qlistwidgettestui, tr("Horizontal Layout"));
}
ui.tabWidget->setCurrentWidget(qlistwidgettestui);
}
}
//隐藏标签
ui.tabWidget->tabBar()->hide();
//隐藏头部
ui.navigationTree->SetHeaderHidden(true);
//设置导航菜单
SetNavigationTree();
//设置导航信号槽
connect(ui.navigationTree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(NavigationToView(QTreeWidgetItem*, int)));