Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Inheritance Class Subclass Problem (simple)

    General and Desktop
    5
    10
    2182
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      MrNoway last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • K
        KA51O last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • M
          MrNoway last edited by

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

          1 Reply Last reply Reply Quote 0
          • L
            lgeyer last edited by

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

            1 Reply Last reply Reply Quote 0
            • M
              MrNoway last edited by

              could you explain it a little bit more?

              which layout?

              1 Reply Last reply Reply Quote 0
              • A
                andre last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • M
                  MrNoway last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • A
                    andre last edited by

                    [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.

                    1 Reply Last reply Reply Quote 0
                    • M
                      MrNoway last edited by

                      Which part?

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

                      This cant be so difficult.

                      1 Reply Last reply Reply Quote 0
                      • D
                        DerManu last edited by

                        [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.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post