真实内容不会从一个QMap复制到另一个QMap

时间:2018-04-03 15:43:37

标签: c++ qt5

当我按下标签窗口小部件上的关闭按钮时,该标签必须关闭,并且必须将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))));
}

1 个答案:

答案 0 :(得分:0)

我看到的问题是你首先从tabWidget中删除选项卡。这会导致索引更改,当您删除选项卡时,下一个选项卡将向左移动。当您尝试使用tabWidget->widget(concreteIndex)访问该标签页时,您可以访问下一个标签页。

您可以考虑在删除之前向deletedWidgetMap添加标签。