Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [Solved]Empty QWidget

    General and Desktop
    2
    6
    2886
    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.
    • Y
      yhu420 last edited by

      Hi everyone,
      Today I just tried a new way to organize my code, just putting away the pointers for a moment, but as I see, this doesn't work at all --'. I'm trying to create a basic C++ class from QWidget and with layouts, but unfortunatly nothings shows up in my window :(.

      Here's the code: (da header)
      @#ifndef MAINWIN_H
      #define MAINWIN_H

      #include <QWidget>
      #include <QLabel>
      #include <QLineEdit>
      #include <QSpinBox>
      #include <QPushButton>
      #include <QTextEdit>
      #include <QVBoxLayout>
      #include <QHBoxLayout>

      class Mainwin : public QWidget
      {
      Q_OBJECT
      public:
      explicit Mainwin(QWidget *parent = 0);

      signals:

      public slots:

      private:

      };

      #endif // MAINWIN_H
      @

      And here's the .cpp:
      @#include "Mainwin.h"

      Mainwin::Mainwin(QWidget *parent) : QWidget(parent)
      {
      QLabel ip; ip.setText(tr("IP du serveur"));
      QLabel servport; servport.setText(tr("Port du serveur"));
      QLabel pseudo; pseudo.setText(tr("Pseudo: "));
      QLabel message; message.setText(tr("Message: "));

      QLineEdit servedit;     servedit.setText("127.0.0.1");
      
      QSpinBox serportspin;   serportspin.setValue(50885);
      
      QPushButton connectbut; connectbut.setText(tr("Connexion"));
      QPushButton sendbut;    sendbut.setText(tr("Envoyer"));
      
      QTextEdit mainte;       mainte.setReadOnly(true);
      QTextEdit pseudoedit;   //settings.value("lastpseudo") TODO
      QTextEdit messageedit;
      
      QVBoxLayout *mainlayout = new QVBoxLayout;
      
      QHBoxLayout *top = new QHBoxLayout;
      QHBoxLayout *bottom = new QHBoxLayout;
      
      top->addWidget(&ip); top->addWidget(&servedit); top->addWidget(&servport); top->addWidget(&serportspin); top->addWidget(&connectbut);
      //mainte
      bottom->addWidget(&pseudo); bottom->addWidget(&pseudoedit); bottom->addWidget(&message); bottom->addWidget(&messageedit); bottom->addWidget(&sendbut);
      
      mainlayout->addLayout(top);
      mainlayout->addWidget(&mainte);
      mainlayout->addLayout(bottom);
      setLayout(mainlayout);
      

      }
      @

      I'm pretty sure I made a stupid mistake somewhere..
      Thank you for every answer you can bring

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        All your widgets go out of scope at the end of the constructor so they are destroyed

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • Y
          yhu420 last edited by

          Ok so I HAVE TO use pointers in my constructor right?
          Thanks for your reply

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Yes, you have to allocate the widgets on the heap.

            Have a look at the examples from the doc, it's all explained

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • Y
              yhu420 last edited by

              Yes, that's not my first use of Qt so I know (more or less) how it works. I just replaced the widgets by pointers and it works, thanks to you ;) .

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                You're welcome !

                I'll encourage you to also get a good book on C++ and Qt to get a better understanding of how it works. It will save you a lot of trouble later

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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