Make Appear A Widget in Same Window



  • Hi Everyone,
    I wish to make appear a widget from a different class in the same window of another widget from another class. My problem is that the widget keep appearing inside a new and different window. Someone knows how to fix it? Here is my code:
    @
    //WindowA.h

    #ifndef WINDOWA_H
    #define WINDOWA_H
    #include <QtGui>
    #include "ToolList.h"

    class WindowA : public QWidget()
    {
    Q_OBJECT
    public:
    WindowA();

    public slots:
    void DisplayListTool();
    
    private:
    QPushButton *BoutonTool ;   ToolList *list;
    

    };
    #endif // WINDOWA_H

    //WindowA.cpp

    #include <QtGui>
    #include "FenetreA.h"
    #include "ToolList.h"

    WindowA::WindowA() : QWidget()
    {
    setFixedSize(1000, 240);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(Qt::FramelessWindowHint);
    BoutonTool = new QPushButton("Tool", this);
    QObject::connect(BoutonTool, SIGNAL(pressed()), this, SLOT(DisplayListTool()));
    }

    void WindowA::DisplayListTool()
    {
    list = new ToolList (this);
    }
    @

    @
    //ToolList.h

    #ifndef TOOLLIST_H
    #define TOOLLIST_H
    #include <QtGui>
    #include "WindowA.h"

    class ToolList : public QWidget
    {
    Q_OBJECT

    public:
    ToolList(QWidget *parent = 0);
    
    private:
    QListWidget *ListTool;
    

    };
    #endif // TOOLLIST_H

    //ToolList.cpp

    #include <QtGui>
    #include "ToolList.h"
    #include "WindowA.h"

    ToolList::ToolList(QWidget *parent) : QWidget(parent)
    {
    ListTool = new QListWidget;
    ListTool->move(0, 10);
    ListTool->setMaximumSize(240, 78);
    ListTool->setSpacing(4);

    QPropertyAnimation *animation = new QPropertyAnimation(ListTool, "geometry");
    animation->setDuration(250);
    animation->setStartValue(QRect(0, 10, 0, 78));
    animation->setEndValue(QRect(0, 10, 240, 78));
    animation->start();
    
    QListWidgetItem *Fil = new QListWidgetItem (ListTool);
    Fil->setText(tr("Fil"));
    Fil->setTextAlignment(Qt::AlignHCenter);
    
    QListWidgetItem *Ram = new QListWidgetItem (ListTool);
    Ram->setText(tr("Ram"));
    Ram->setTextAlignment(Qt::AlignHCenter);
    
    QListWidgetItem *FlipFlop = new QListWidgetItem (ListTool);
    FlipFlop->setText(tr("FlipFlop"));
    FlipFlop->setTextAlignment(Qt::AlignHCenter);
    
    ListTool->show();
    

    }
    @



  • Hi, First of all, you might have a big memory leak! Everytime your BoutonTool button is pressed you generate a new ToolList, but you do not remove the old one!! So the old allocated memory will not be released to the rest of the program and you memory will run full if used to often during a single program run.
    What I think you are trying to do is to add the ToolList widget to the FenetreA window? If so, you should place a layout in your FenetreA widget and when the button is pressed and you add the ToolList widget you add the widget to the layout.
    The only think what you do now is that you create a widget of the type ToolList and set the parent. This does NOT imply that the widget should be displayed inside that widget, only that the QObject part of the FenetreA class is responsible (parent) of the ToolList widget!
    Also just a short tip, but do not use ToolList and ListTool for different items, it is very confusing and makes the code less readable.
    Hope this helps!



  • Hi Jeroentje, thanks a lot for your reply and advises.

    bq. What I think you are trying to do is to add the ToolList widget to the FenetreA window?

    Yes I confirm, it is what I want to do.
    Ok, I try your idea with the layout inside WindowA. I'll give a feeback.
    In your first paragraph (memory leak), you suggest then to add a destructor in my class WindowA?
    For the "this" I get it, thanks.


  • Lifetime Qt Champion

    Hi,

    No, Jeroentje@home was suggesting to

    @
    if (list)
    list->deleteLater();
    list = new ToolList;
    @

    But if your idea is just to hide/show your list widget, you should rather create it once and call show()/hide() on it when needed.



  • Hi SGaist, thanks for the tip concerning ToolList. For the rest I am confused, isn't it what I am doing?! If not, can you show me please because I don't get it.

    Anyway, I still have problems to display the listwidget inside "windowA" . When I click on my pushbutton the whole application close, more precisely crashes. Here is all I have changed, (ToolList.cpp and ToolList.h stay the same):
    @
    //WindowA.h

    #ifndef WINDOWA_H
    #define WINDOWA_H
    #include <QtGui>
    #include "ToolList.h"

    class WindowA : public QWidget()
    {
    Q_OBJECT
    public:
    WindowA();

    public slots:
    void DisplayListTool();
    
    private:
    QPushButton *BoutonTool ;   ToolList *list;  QVBoxLayout *layout1;
    

    };
    #endif // WINDOWA_H

    //WindowA.cpp

    #include <QtGui>
    #include "FenetreA.h"
    #include "ToolList.h"

    WindowA::WindowA() : QWidget()
    {
    setFixedSize(1000, 240);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(Qt::FramelessWindowHint);
    BoutonTool = new QPushButton("Tool", this);
    QObject::connect(BoutonTool, SIGNAL(pressed()), this, SLOT(DisplayListTool()));

    layout1 = new QVBoxLayout;
    }

    void WindowA::DisplayListTool()
    {
    layout1->addWidget(list);
    setLayout(layout1);
    }
    @


  • Lifetime Qt Champion

    I would recommend that you have a look at the examples from Qt's documentation on how to create a custom widget.

    @
    //WindowA.h

    #ifndef WINDOWA_H
    #define WINDOWA_H
    #include <QWidget>

    class ToolList

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

    public slots:
    void displayListTool();
    
    private:
        ToolList *_toolList;
    

    };
    #endif // WINDOWA_H

    //WindowA.cpp

    #include <QPushButton>
    #include <QVBoxLayout>

    #include "windowa.h"
    #include "toollist.h"

    WindowA::WindowA(QWidget *parent) :
    QWidget(parent),
    _toolList(new ToolList)
    {
    setFixedSize(1000, 240);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(Qt::FramelessWindowHint);

    _toolList->hide();

    QPushButton *buttonTool = new QPushButton(tr("Tool"));
    connect(buttonTool, SIGNAL(pressed()), SLOT(displayListTool()));

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(buttonTool);
    layout->addWidget(_toolList);
    }

    void WindowA::displayListTool()
    {
    _toolList->show();
    }
    @

    Not tested/compiled but shows the idea



  • Thanks SGaist for your example. However I tried it and other variants and my problem still remains. Worst, your code and the variants open the qlistwidget without pressing BoutonTool.


  • Lifetime Qt Champion

    Are you talking about ListTool in ToolList ?

    Then you should really look at Qt's examples about the use of layouts and parent/child relationship



  • No, I was talking about WindowA.cpp and WindowA.h . Concerning use of layouts and parent/child relationship with custom widget (or different classes), if you have links I'll be happy to see them because I have found any.

    Other Queries: whatever the size I choose with "setFixedSize();" in ToolList.cpp, it doesn't work. Someone knows Why? (Obviously I have removed the animation and setMaximumSize)


  • Lifetime Qt Champion



  • Sorry for the delay. It is not really what I wanted but thanks.


Log in to reply
 

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