【问题标题】:Is it possible to rename a qt sub window?是否可以重命名 qt 子窗口?
【发布时间】:2015-04-16 14:19:31
【问题描述】:

我在主窗口的 QMdiArea 中设置了一个子窗口。然后我制作了一个 QDialog,我希望用户在其中输入子窗口的标题名称。但是在尝试将 windowTitle() 更改为该变量时,我总是遇到错误。

有什么方法可以更新 windowTitle() 吗?

模块名称.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();
}

模块名称.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

感谢您的帮助。

【问题讨论】:

    标签: c++ qt qdialog qmdiarea


    【解决方案1】:

    windowTitle() 返回一个副本,所以这一行没有用(你只是在修改副本):

    window->windowTitle() = name; 
    

    相反,直接使用setWindowTitle

    window->setWindowTitle(name);
    

    【讨论】:

    • 我仍然收到错误消息:iwb4.exe 中 0x64f4461e (QtGuid4.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0x0000004d。
    • @hbrown 使用 Qt 的调试版本并简单地在调试器中运行它,您会希望看到它为什么试图读取不是地址的内容。
    【解决方案2】:

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

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

    window->setWindowTitle(window->windowTitle());没用的语句,因为你又把窗口的标题设置为原来的标题了。

    【讨论】:

      【解决方案3】:
      void iwb4::name()
      {   
          QString name;
          name = p_name;
          QMdiSubWindow* window;
          window = m_subWindow;
          window->windowTitle() = name;
          window->setWindowTitle(window->windowTitle());
      }
      

      window-&gt;windowTitle( ) 是一个吸气剂。

      window-&gt;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()));
      }
      

      【讨论】:

      • 我仍然收到此错误消息:iwb4.exe 中 0x64f4461e (QtGuid4.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0x0000004d。
      • m_subWindow 是一个实际的 QMdiSubWindow 吗?请显示您的定义和声明。
      • @hbrown 使用更改后的代码再试一次,如果仍然不起作用,您的问题出在代码中的其他地方。在name = "MyNewWindowName"; 设置断点并检查程序是否实际运行了那么远或崩溃的地方。
      • 它运行到那个点并且错误出现在 qwidget.cpp QString QWidget::windowTitle() const { Q_D(const QWidget); if (d-&gt;extra &amp;&amp; d-&gt;extra-&gt;topextra) { if (!d-&gt;extra-&gt;topextra-&gt;caption.isEmpty()) return d-&gt;extra-&gt;topextra-&gt;caption; if (!d-&gt;extra-&gt;topextra-&gt;filePath.isEmpty()) return constructWindowTitleFromFilePath(d-&gt;extra-&gt;topextra-&gt;filePath); } return QString(); }
      • 是因为ist 声明为const 吗?你知道有什么其他方法可以让用户改变窗口标题吗?
      猜你喜欢
      • 1970-01-01
      • 2010-10-08
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2012-06-01
      • 2013-02-22
      相关资源
      最近更新 更多