gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:38 25 4
gpt4 key购买 nike

我在主窗口的 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

enter image description here

感谢您的帮助。

最佳答案

windowTitle() 返回一个拷贝,所以这一行是无用的(你只是在修改拷贝):

window->windowTitle() = name; 

相反,直接使用setWindowTitle:

window->setWindowTitle(name);

关于c++ - 是否可以重命名 qt 子窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29677523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com