tags:

views:

51

answers:

1

Hi,

I'm trying to collect an often used subset of GUI-Elements together into one Subclass, which can be "included" into the real GUIs later without rewriting the given functionality (don't ask why, I wanna learn it for later use). The Subclass should use it's own *.ui-File and should be put into an QWidget resding in the real GUI. After this, it would be nice to access some methods of the Subclass from the real GUI -- like the state of a button or so.

But how do I do this right?

In the moment, my Subclass works and is instantiated in main, but cannot be accessed from the real GUI because its only declared in main.

My Subclass Header-File:

class logger : public QWidget, private Ui::loggerWidget {
    Q_OBJECT

public:
    logger(QWidget *parent = 0);
    virtual ~logger();
    // some more stuff...
}

The corresponding constructor. I had to run setupUI with "parent" instead of "this", but I'm not sure that this is correct -- anyways, it works... otherwise, the subelements from the subclass are not shown in the main-window of the real GUI.

logger::logger(QWidget *parent) : QWidget(parent){
    setupUi(parent);
    //ctor
}

Inside the main.cpp the main-window is constructed, which uses it's own *.ui-File (containing one widget "widget_loggerArea") aswell. Doing so, I can not access methods of "logger" from within "loggerTest":

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    loggerTest window;

    logger myLog(window.widget_loggerArea);

    window.show();

    return app.exec();
}

I can't put the constructor of "logger" into the constructor of the main-window "loggerTest", since it will be destroyed immidiately and never enters the event-loop.

I'm sure I'm missing some concept of object-oriented programming, or the way qt handles its stuff... I would be gratefull if someone could put my nose to this ;-)

Thanks

Marvin

EDIT:

Oh fuck, I was so stupid... using a pointer with new and delete does the job... this is so silly, I can't believe it! I'm more used to VHDL recently, this weakens my c++-karma...

A: 

So, the answer is in the real GUI class. The Constructor:

testLogger::testLogger(QMainWindow *parent) : QMainWindow(parent){
    setupUi(this);

    myLog = new logger(widget_loggerArea);
}

In main.cpp:

QApplication app(argc, argv);
testLogger window;
window.show();

And in constructor of logger, setupUi works with "this":

dfkiLogger::dfkiLogger(QWidget *parent) : QWidget(parent){
    setupUi(this);
}

Yes, thats it... Just for completebility, maybe someone needs a similar "push in the right direction"...

EDIT: In the header of the SubClass the scope of the ui-Elements has to be updated to "public", too:

class logger : public QWidget, public Ui::loggerWidget {
    Q_OBJECT

    public:
        logger(QWidget *parent = 0);
        virtual ~logger();
}
marvin2k
There is only one missing. You had said that you wanted to be able to access the state of the objects in logger UI. If you still need that, then in the header for logger, then change the scope of ui variable from private to public.
photo_tom
Ah thanks, you are right -- did this locally, forgot to mention it, corrected my answer...
marvin2k