Important: Please read the Qt Code of Conduct -

[Solved]Empty QWidget

  • 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
    explicit Mainwin(QWidget *parent = 0);


    public slots:



    #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("");
    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);
    bottom->addWidget(&pseudo); bottom->addWidget(&pseudoedit); bottom->addWidget(&message); bottom->addWidget(&messageedit); bottom->addWidget(&sendbut);


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

  • Lifetime Qt Champion


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

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

  • Lifetime Qt Champion

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

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

  • 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 ;) .

  • Lifetime Qt Champion

    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

Log in to reply