Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Empty QWidget
QtWS25 Last Chance

[Solved]Empty QWidget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.4k Views
  • 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 Offline
    Y Offline
    yhu420
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • Y Offline
        Y Offline
        yhu420
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • Y Offline
            Y Offline
            yhu420
            wrote on last edited by
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved