0

I have a problem with accessing ui elements from another class(with instance). I have a second QMainWindow in my application, I can access in secondWindow.cxx class all ui elements but not in read.cxx class. My code looks like following. Where is my mistake? Thank you for your help.

-------------------------------secondWindow.h------------------------------------

#ifndef __secondWindow_h
#define __secondWindow_h

#include "ui_secondwindow.h"

class secondWindow : public QMainWindow
{
    friend class read;
    igstkStandardClassBasicTraitsMacro(secondWindow, QMainWindow);
    Q_OBJECT

public:
    igstkStateMachineMacro();

    secondWindow();
    virtual ~secondWindow();
    void createSignalAndSlots();

public slots:
    void secondWindowTest();

protected:

private:
    Ui::secondMainWindow m_secondWindowUI;
};
#endif

-------------------------------secondWindow.cxx------------------------------------

#include "secondWindow.moc"
#include "secondWindow.h"
#include "read.h"

secondWindow::secondWindow() :m_StateMachine(this)
{
    m_secondWindowUI.setupUi(this);
    createSignalAndSlots();
}

void secondWindow::createSignalAndSlots()
{
    connect(m_secondWindowUI.pushButton1, SIGNAL(clicked()),this, SLOT(secondWindowTest()));

    connect(m_secondWindowUI.pushButton2, SIGNAL(clicked()), read::instance(), SLOT(readTest()));
}

void secondWindow::secondWindowTest()
{
    m_secondWindowUI.pushButton1->setEnabled(true); //OK
}

secondWindow::~secondWindow(){}


---------------------------------read.h--------------------------------------

#pragma once

#include "secondWindow.h"

class read : public QObject
{
    Q_OBJECT

public:
    static read *instance();
    read();
    virtual ~read() {}

public slots:
    void readTest();

protected:
    secondWindow    *m_readUI;
    static read     *m_read;

private:
};

---------------------------------read.cxx--------------------------------------
#include <read.moc>
#include "secondWindow.h"
#include "read.h"

read *read::m_read= NULL;

read::read()
{
    m_readUI = dynamic_cast<secondWindow*>( QApplication::instance() );
}

read *read::instance()
{
    if(m_read == NULL)
    m_read = new read();

    return m_read;
}

void read::readTest()
{
    m_readUI->m_secondWindowUI.qlabelTest->setText("test"); //segmentation fault
}

1 Answers1

0

You are casting a QApplication::instance(), which is a QApplication * deriving from QCoreApplication * deriving from QObject *. That won't work, it's not a secondWindow *, not even a QMainWindow *, not even a QWidget *.

Apart from that, your coding style is rather strange -- in Qt, it's customary to use CamelCase for classes, not thisStuff which usually applies to functions and methods. Including <read.moc> is just wrong. Why is read::m_read static? Finally, the coupling between the two window classes is set up in a strange way (accessing global stuff like QApplication just to get a reference to another window smells ugly code). A much better and more obvious approach is to either wrap all of your windows in a parent object or setting up the dependencies explicitly, perhaps like this:

MainWindow *mainWindow = new MainWindow();
SecondWindow *second = new SecondWindow(mainWindow);
UtilityWindow *utilityWin = new UtilityWindow(second);
Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29