简体   繁体   中英

True content is not copied from one QMap into another QMap

When I press the close button on the tab widget, that tab must close, and the corresponding member of the identifyedWidgetMap must be copied into the deletedWidgetMap . However, the copying does not happen.

For instance, I close Tab_3 but there is in the deletedWidgetMap some information: 4, QWidget(0x17424a1) .

Help if you can. Thank you.

That code lines I created are located below.

Header

#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

Source

#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))));
}

The problem I see is you first remove the tab from the tabWidget. This results in an index change, the next tab will shift to left when you remove the tab. When you try to access the tab with tabWidget->widget(concreteIndex) you access the next tab.

You may consider adding tab to the deletedWidgetMap before removing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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