2

I set up a sub windwow in the QMdiArea of my mainwindow. Then I made a QDialog in which I want the user to enter the title name for the Sub window. But I always get an error when trying to Change the windowTitle() to that variable.

Is there any way to update the 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

Thanks for your help.

hbrown
  • 41
  • 4

3 Answers3

0

windowTitle() returns a copy, so this line is useless (you are modifying the copy only):

window->windowTitle() = name; 

Instead, just use setWindowTitle directly:

window->setWindowTitle(name);
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • I still get the error message though: Unhandled exception at 0x64f4461e (QtGuid4.dll) in iwb4.exe: 0xC0000005: Access violation reading location 0x0000004d. – hbrown Apr 16 '15 at 14:34
  • @hbrown Use a debug build of Qt and simply run it in the debugger, you'll hopefully see why it's trying to read what isn't an address. – Kuba hasn't forgotten Monica Apr 16 '15 at 16:14
0

windowTitle() simply returns just another copy of the title of the window, so changing it does not change the title you want to change.

If you need to change the title, then directly use setWindowTitle(name); .

window->setWindowTitle(window->windowTitle()); is useless statement, because you set the title of window to its original title again.

OrkhanIO
  • 33
  • 1
  • 7
0
void iwb4::name()
{   
    QString name;
    name = p_name;
    QMdiSubWindow* window;
    window = m_subWindow;
    window->windowTitle() = name;
    window->setWindowTitle(window->windowTitle());
}

window->windowTitle( ) is a getter.

window->setWindowTitle( ) is a setter.

The getters job is to get you the value whereas the setters job is to update the value.

So running this will work:

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

You have the same problem here:

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

Change it to:

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()));
}
deW1
  • 5,562
  • 10
  • 38
  • 54
  • I still get this error message though: Unhandled exception at 0x64f4461e (QtGuid4.dll) in iwb4.exe: 0xC0000005: Access violation reading location 0x0000004d. – hbrown Apr 16 '15 at 14:44
  • is m_subWindow an actual QMdiSubWindow? Show your definition and declaration of it please. – deW1 Apr 16 '15 at 14:46
  • @hbrown try it again with the changed code, if that still doesn't work your problem lies somewhere else in your code. set a breakpoint at `name = "MyNewWindowName";` and check if the program actually runs that far or where it crashes. – deW1 Apr 16 '15 at 15:08
  • it runs to that point and the error Comes at the qwidget.cpp `QString QWidget::windowTitle() const { Q_D(const QWidget); if (d->extra && d->extra->topextra) { if (!d->extra->topextra->caption.isEmpty()) return d->extra->topextra->caption; if (!d->extra->topextra->filePath.isEmpty()) return constructWindowTitleFromFilePath(d->extra->topextra->filePath); } return QString(); }` – hbrown Apr 16 '15 at 15:17
  • Is that because ist declared as `const` ? do you know any other way to have the window title changeable for the user? – hbrown Apr 16 '15 at 15:18
  • @hbrown no that's got nothing to do with it, but you had another mistake in your code. You need to make sure you're not calling the `windowTitle( )` and giving it a value. Check the rest of your code and replace it with `setWindowTitle( )` also it's const for a good reason like someone like you trying to abuse the method :p – deW1 Apr 16 '15 at 15:26
  • I already removed that, but it's still giving me the same error message. – hbrown Apr 16 '15 at 15:49
  • @hbrown when debugging your code at which call of your code does it jump out? it's only calling functions by your calls so i need to know which call is making `QWidget::windowTitle()` because I'm sure you're somewhere still calling that function. – deW1 Apr 16 '15 at 16:19
  • the call is `m_subWindow->setWindowTitle(name);` in the `void iwb4::name()` – hbrown Apr 16 '15 at 16:24
  • added a picture, if that's any help – hbrown Apr 16 '15 at 16:26
  • @hbrown i don't see it other than there seem to be more problems, can you maybe upload the whole thing so i can take a look? – deW1 Apr 16 '15 at 16:32
  • @hbrown sorry i meant as zip somewhere so i can actually run it. – deW1 Apr 16 '15 at 17:07
  • @hbrown so the problem is with `iwb4 iwb;` when you do that you create a new instance of the class meaning `m_subWindow` will point to who knows what – deW1 Apr 16 '15 at 19:14
  • so how do I fix that? – hbrown Apr 16 '15 at 20:00
  • @hbrown I would ask a new question, since this question is about setting a WindowTitle which has been answered. What you want to do is access an already defined class member from a sub class. Also I would not use a different class for that but instead implement your moduleName in the main class or create the class there and use getter and setter. Basically you either need to get the pointer of the class subWindow or the main object of the class to access its subWindow pointer – deW1 Apr 16 '15 at 20:18