Inheritance Class Subclass Problem (simple)



  • Hi everyone.
    I have little problem that kills me.

    I have one main class and a subclass that is derived fromt he main class
    main class Lines
    subclass Point

    @class Lines : public QMainWindow{
    Q_OBJECT
    public:
    QWidget *widget_map_bg;
    Lines();

    ################################################

    Lines::Lines(QWidget *parent) : QMainWindow ( parent ){
    ui.setupUi(this);
    widget_map_bg = new QWidget(ui.centralwidget);
    }@

    AND

    @
    class Point : Lines(){
    Q_OBJECT
    public:
    Point();
    QLabel *label;
    };

    ################################################

    Point::Point(){
    label = new QLabel(widget_map_bg);
    label->setText("HALLO");

    }

    @

    My Problem is, the text does not show up.
    I tried also using pixmap and other things but nothing wants to be dispalyed

    What did I do wrong?

    thanks

    if I exchange the code with this
    @ label = new QLabel();
    label->show@

    then a seperate widget is created and displayed,
    so the code is actually working.



  • Where do you call the constructor of your Point class and add the label to you mainwindow? To answer your question we need to see more code. All I can deduce is that the label from your Point class has widget_map_bg as its parent.



  • hi, thanks for your answer,

    I just tried to reduce as much as possible making it easier to overview.

    okay here is the main

    main.cpp

    @
    int main( int argc, char *argv[] )
    {
    QApplication app( argc, argv);
    Lines w;
    w.resize(1280,720);
    w.move(100, 100);

    Point p;
    w.show();

    return app.exec();
    }@



  • Both widgets, <code>widget_map_bg</code> and <code>label</code>, have not been added to a layout.



  • could you explain it a little bit more?

    which layout?



  • I think you totally misunderstand subclassing, MrNoway.
    Why do you think Lines should inherit QMainWindow, and points should inherit Lines? Do you really mean to express that a Point is a Lines, which is a QMainWindow? That shound just weird to me.

    I'd think that all you want to do is have your QMainWindow render some lines and some points. But that does not require anything like you're trying to do here.

    Please get yourself a decent C++ book to get familiar with the basics a bit. Without understanding the basics of C++, you're not going to be able to work effectively with Qt either.



  • I just used a simple example to make it clearly what I want.



  • [quote author="MrNoway" date="1359713487"]I just used a simple example to make it clearly what I want.
    [/quote]
    If you did, then you failed. It only managed to confuse me, obviously.



  • Which part?

    I simply want to make a QLabel parent over a QWidget from another class.

    This cant be so difficult.



  • [quote author="MrNoway" date="1359714403"]I simply want to make a QLabel parent over a QWidget from another class. This cant be so difficult.[/quote]
    Probably the other way around, right? i.e. something like label.setParent(widget).

    You're right. It's not so difficult. But you just lack C++/Qt basics, so it would benefit you much more to learn them in a systematic manner (e.g. a book or an extensive read in the Qt documentation) than asking specific questions in fora. Anyhow, as André has said, subclassing is the wrong tool for parenting. Have a look at the QObject parenting system.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.