是否可以重命名qt子窗口?

时间:2015-04-16 14:19:31

标签: c++ qt qdialog qmdiarea

我在主窗口的QMdi区域设置了一个子风向。然后我做了一个QDialog,我希望用户输入Sub窗口的标题名称。但是在尝试将windowTitle()更改为该变量时,我总是遇到错误。

有没有办法更新windowTitle()?

moduleName.cpp

#include "stdafx.h"
#include "moduleName.h"
#include "iwb4.h"
#include <Windows.h>
#include <QtGui/QAction>
#include <qdom.h>
#include <qmdiarea.h>
#include "ui_module_name.h"
#include "ui_iwb4.h"
#include <qmdisubwindow.h>

moduleName::moduleName(QDialog *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
    show();

    // connect ok button to save the module name
    connect(ui.okButton, SIGNAL(pressed()), this, SLOT(okClicked()));
}

moduleName::~moduleName()
{

}

void moduleName::okClicked()
{
    iwb4 iwb;
    QTextDocument* tName = ui.textEdit->document(); 
    iwb.p_name = tName->toPlainText();

    moduleName::close();

    iwb.name();
}

moduleName.h

#ifndef MODULENAME_H
#define MODULENAME_H

#include <QtGui/QWidget>
#include "ui_module_name.h"


class moduleName : public QDialog
{
    Q_OBJECT

public:
    moduleName(QDialog *parent = 0);
    ~moduleName();

public slots:
    void okClicked();

protected:
    Ui::Dialog ui;

};

#endif // MODULENAME_H

iwb4.cpp

#include "stdafx.h"
#include "iwb4.h"
#include <Windows.h>
#include "ui_iwb4.h"
#include <QtGui/QAction>
#include <qdom.h>
#include <qmdiarea.h>
#include "ui_module_name.h"
#include "moduleName.h"
#include <qmdisubwindow.h>


iwb4::iwb4(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    iwb4::showMaximized();

    p_name = " ";

    // new module button
    connect(ui.actionNewModule, SIGNAL(triggered()), this, SLOT(makeModule()));
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

iwb4::~iwb4()
{

}


void iwb4::newModule(QString name)
{
    m_file = new QFile("C:\\Users\\Hanna\\Desktop\\iwb\\Projekt\\iwb4\\iwb4\\Datenmodell\\module.xml");
    m_file->open(QFile::ReadWrite | QFile::Text);
    QDomDocument doc;
    doc.setContent(m_file); 
    m_file->close();

    m_dockWidget = new QDockWidget(m_parent);
    m_dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea);
    m_dockWidget->showMaximized();
    m_dockWidget->setTitleBarWidget(new QWidget());
    m_pTableWidget = new QTableWidget(m_dockWidget);
    m_dockWidget->setWidget(m_pTableWidget);
    addDockWidget(Qt::LeftDockWidgetArea, m_dockWidget);

    m_pTableWidget->setRowCount(10);

    QDomElement elem = doc.documentElement();
    m_pTableWidget->setColumnCount(elem.childNodes().count());
    for (int i = 0; i < elem.childNodes().count(); i++)
    {
        QString header = elem.childNodes().at(i).toElement().attribute("Name");
        m_TableHeader += header;
    }

    m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader);
    m_pTableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    m_pTableWidget->verticalHeader()->setVisible(false);
    m_pTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_pTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    m_pTableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pTableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pTableWidget->setShowGrid(true);
    m_pTableWidget->resizeColumnsToContents();
    m_pTableWidget->resizeRowsToContents();
    m_pTableWidget->setMaximumWidth(400);
    m_pTableWidget->setMaximumHeight(300);

    connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ),
        this, SLOT( cellSelected( int, int ) ) );
}

void iwb4::makeModule()
{
    QString name;
    name = p_name;

    newModule(name);

    QDockWidget *dock = m_dockWidget;
    m_subWindow = ui.mdiArea->addSubWindow(dock);
    ui.mdiArea->DontMaximizeSubWindowOnActivation;
    dock->show();
    dock->activateWindow();

    // make rename option in right click menu
    QMenu *menu = m_subWindow->systemMenu();
    rename = new QAction(tr("Rename"),menu);
    menu->addAction(rename);
    connect(rename, SIGNAL(triggered()), this, SLOT(newName()));
}

void iwb4::newName()
{
    moduleName* p_nameDialog = new moduleName();
}

void iwb4::name()
{   
    QString name = p_name;
    m_subWindow->setWindowTitle(name);
}

iwb4.h

#ifndef IWB4_H
#define IWB4_H

#include <QtGui/QMainWindow>
#include "ui_iwb4.h"

class iwb4 : public QMainWindow
{
    Q_OBJECT

public:
    iwb4(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~iwb4();

private: 

private slots: 
    void makeModule();

public slots:
    void newName();

public:
    void newModule(QString name);
    void name();

    QFile* m_file;
    QDockWidget* m_dockWidget;
    QTableWidget* m_pTableWidget;
    QMdiSubWindow* m_subWindow;
    QStringList m_TableHeader;

    QString p_name;

    QAction *rename;

protected:
    Ui::iwb4Class ui;

};

#endif // IWB4_H

enter image description here

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

windowTitle()会返回一个副本,因此这行无用(您只修改副本):

window->windowTitle() = name; 

相反,只需直接使用setWindowTitle

window->setWindowTitle(name);

答案 1 :(得分:0)

windowTitle()只返回窗口标题的另一个副本,因此更改它不会更改要更改的标题。

如果需要更改标题,则直接使用setWindowTitle(name);

窗口 - &GT; setWindowTitle(窗口 - &GT; WINDOWTITLE());是无用的声明,因为你再次将窗口的标题设置为其原始标题。

答案 2 :(得分:0)

void iwb4::name()
{   
    QString name;
    name = p_name;
    QMdiSubWindow* window;
    window = m_subWindow;
    window->windowTitle() = name;
    window->setWindowTitle(window->windowTitle());
}

window->windowTitle( )是一个吸气剂。

window->setWindowTitle( )是一个制定者。

getter的工作是获取值,而setter的工作是更新值。

所以运行它会起作用:

void iwb4::name()
{   
    QString name;
    name = "MyNewWindowName";
    m_subWindow->setWindowTitle( name );
}

你在这里遇到同样的问题:

void iwb4::makeModule()
{
    QString name;
    name = p_name;

    newModule(name);

    QDockWidget *dock = m_dockWidget;
    m_subWindow = ui.mdiArea->addSubWindow(dock);
    ui.mdiArea->DontMaximizeSubWindowOnActivation;
    ui.mdiArea->windowTitle() = name;
    ui.mdiArea->setWindowTitle(ui.mdiArea->windowTitle());
    dock->show();
    dock->activateWindow();

    // make rename option in right click menu
    QMenu *menu = m_subWindow->systemMenu();
    rename = new QAction(tr("Rename"),menu);
    menu->addAction(rename);
    connect(rename, SIGNAL(triggered()), this, SLOT(newName()));
}

将其更改为:

void iwb4::makeModule()
{
    QString name;
    name = p_name;

    newModule(name);

    QDockWidget *dock = m_dockWidget;
    m_subWindow = ui.mdiArea->addSubWindow(dock);
    ui.mdiArea->DontMaximizeSubWindowOnActivation;
    //ui.mdiArea->windowTitle() = name; // <--
    ui.mdiArea->setWindowTitle(name); // <--
    dock->show();
    dock->activateWindow();

    // make rename option in right click menu
    QMenu *menu = m_subWindow->systemMenu();
    rename = new QAction(tr("Rename"),menu);
    menu->addAction(rename);
    connect(rename, SIGNAL(triggered()), this, SLOT(newName()));
}