繁体   English   中英

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

[英]True content is not copied from one QMap into another QMap

当我按下上的标签插件的关闭按钮,该标签必须关闭,并且相应部件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中删除了该标签。 这将导致索引更改,删除标签时下一个标签将向左移动。 当您尝试使用tabWidget->widget(concreteIndex)访问选项卡时,您将访问下一个选项卡。

您可以考虑在删除之前将选项卡添加到deletedWidgetMap

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM