当我按"选项卡窗口小部件上的关闭"按钮时,该选项卡必须关闭,并且必须将identifyedWidgetMap
的相应成员复制到deletedWidgetMap
中。但是,复制不会发生。
例如,我关闭Tab_3
,但是deletedWidgetMap
中有一些信息: 4, QWidget(0x17424a1)
。
如果可以的话,请提供帮助。谢谢。
我创建的代码线位于下方。
标题
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextEdit>
#include <QMap>
#include <QList>
#include <iostream>
#include <QDebug>
#include <QString>
#include <QFont>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void setMainWindowPropertiesAndItems();
QMenuBar* menuBar;
QMenu* fileMenu;
QAction* newTabAction;
QAction* openAction;
QTabWidget* tabWidget;
QMap<int, QWidget*> identifyedWidgetMap;
QMap<int, QWidget*> deletedWidgetMap;
QList<QTextEdit*> textEditList;
QVBoxLayout* vBoxLayout;
private slots:
void newTabActionHandler();
void openActionHandler();
void tryingToCloseConcreteTab(int);
};
#endif // MAINWINDOW_H
来源
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setMainWindowPropertiesAndItems();
}
MainWindow::~MainWindow()
{
}
void MainWindow::setMainWindowPropertiesAndItems()
{
setWindowTitle("Notepad");
setGeometry(0, 0, 500, 250);
move(270, 270);
menuBar = new QMenuBar(this);
setMenuBar(menuBar);
fileMenu = new QMenu("&File", this);
menuBar->addMenu(fileMenu);
newTabAction = new QAction("&New Tab", this);
fileMenu->addAction(newTabAction);
connect(newTabAction, SIGNAL(triggered()), this,
SLOT(newTabActionCommandsHandler()));
openAction = new QAction("&Open", this);
fileMenu->addAction(openAction);
connect(openAction, SIGNAL(triggered()), this,
SLOT(openActionCommandsHandler()));
identifyedWidgetMap.insert(0, new QWidget(this));
textEditList.append(new QTextEdit(this));
tabWidget = new QTabWidget(this);
tabWidget->addTab(identifyedWidgetMap.value(0), QString("Tab
%1").arg(identifyedWidgetMap.size()-1));
tabWidget->setMovable(true);
tabWidget->setTabsClosable(true);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this,
SLOT(tryingToCloseConcreteTab(int)));
vBoxLayout = new QVBoxLayout();
identifyedWidgetMap.value(0)->setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[0]);
textEditList[0]->setCurrentFont(QFont("Monospace Regular", 14));
setCentralWidget(tabWidget);
}
void MainWindow::newTabActionCommandsHandler()
{
if(bool(deletedWidgetMap.isEmpty()) == true)
{
identifyedWidgetMap.insert(identifyedWidgetMap.size(), new
QWidget(this));
textEditList.append(new QTextEdit(this));
tabWidget->
addTab(identifyedWidgetMap.value(identifyedWidgetMap.size()-1),
QString("Tab %1").arg(identifyedWidgetMap.size()-1));
tabWidget->
setCurrentWidget(identifyedWidgetMap.
value(identifyedWidgetMap.size()-1));
vBoxLayout = new QVBoxLayout();
identifyedWidgetMap.value(identifyedWidgetMap.size()-1)->
setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[textEditList.size()-1]);
textEditList[textEditList.size()-1]->setCurrentFont(QFont("Monospace
Regular", 14));
}
}
void MainWindow::openActionCommandsHandler()
{
QWidget* currentWidget = tabWidget->currentWidget();
std::cout<<"The currentWidget address is: "<<currentWidget<<std::endl;
qDebug()<<"The complete identifyedWidgetMap address set is: ";
foreach(int widgetIdentifyer, identifyedWidgetMap.keys())
{
qDebug()<<widgetIdentifyer<<", "
<<identifyedWidgetMap.value(widgetIdentifyer);
}
int currentWidgetIndex = 0;
currentWidgetIndex = tabWidget->currentIndex();
std::cout<<"The currentWidgetIndex is: "<<currentWidgetIndex<<std::endl;
qDebug()<<"The complete deletedWidgetMap set is: ";
foreach(int widgetIdentifyer, deletedWidgetMap.keys())
{
qDebug()<<widgetIdentifyer<<", "
<<deletedWidgetMap.value(widgetIdentifyer);
}
}
void MainWindow::tryingToCloseConcreteTab(int concreteIndex)
{
tabWidget->removeTab(concreteIndex);
std::cout<<"Deleted identifyer is: "<<concreteIndex<<std::endl;
deletedWidgetMap.insert(identifyedWidgetMap.key(tabWidget-
>widget(concreteIndex)),
identifyedWidgetMap.value(identifyedWidgetMap.key(tabWidget-
>widget(concreteIndex))));
}
我看到的问题是您首先从选项卡威特删除选项卡。这会导致索引更改,当您删除选项卡时,下一个选项卡将向左移动。当您尝试使用tabWidget->widget(concreteIndex)
访问选项卡时,您可以访问下一个选项卡。
您可以在删除它之前考虑将选项卡添加到deletedWidgetMap
。