簡體   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