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. Make Appear A Widget in Same Window

Make Appear A Widget in Same Window

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 3.0k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    amonR
    wrote on last edited by
    #1

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

    }
    @

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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!

      Greetz, Jeroen

      1 Reply Last reply
      0
      • A Offline
        A Offline
        amonR
        wrote on last edited by
        #3

        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.

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

          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.

          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
          • A Offline
            A Offline
            amonR
            wrote on last edited by
            #5

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

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

              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

              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
              • A Offline
                A Offline
                amonR
                wrote on last edited by
                #7

                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.

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

                  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

                  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
                  • A Offline
                    A Offline
                    amonR
                    wrote on last edited by
                    #9

                    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)

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

                      "Layout management":http://qt-project.org/doc/qt-5/layout.html
                      "Parent child":http://qt-project.org/doc/qt-4.8/objecttrees.html

                      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
                      • A Offline
                        A Offline
                        amonR
                        wrote on last edited by
                        #11

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

                        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