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. Using custom QWidgets in QListWidgets
Forum Updated to NodeBB v4.3 + New Features

Using custom QWidgets in QListWidgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 4 Posters 5.1k Views 3 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.
  • H Offline
    H Offline
    Hubbard
    wrote on last edited by
    #3

    I don't understand :/
    The declaration for that function is:-
    void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)
    and its description is sets the widget in the item
    which I assume means it puts my custom widget inside the QLWI so that it can be added to the list?

    mrjjM 1 Reply Last reply
    0
    • H Hubbard

      I don't understand :/
      The declaration for that function is:-
      void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)
      and its description is sets the widget in the item
      which I assume means it puts my custom widget inside the QLWI so that it can be added to the list?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Hubbard
      Oh, sorry, misread the names it seems.
      But if u insert exampleUnitAddress->ribbonAddress
      are you inserting a widget that lives in other widget ?
      When you insert a QButton its not inside other widget.
      That seems to be the difference ?

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hubbard
        wrote on last edited by
        #5

        The QWidget class 'UnitRibbon' (or UnitRibbon *ribbonAddress) is a member class of BioBloomUnit QObject, with a designed UI

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

          Hi,

          Why are you trying to show an inner widget rather than the full BioBloomUnit widget ?

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

          H 1 Reply Last reply
          2
          • SGaistS SGaist

            Hi,

            Why are you trying to show an inner widget rather than the full BioBloomUnit widget ?

            H Offline
            H Offline
            Hubbard
            wrote on last edited by
            #7

            @SGaist
            The BioBloomUnit isn't a widget with its own graphics (its a QObject), it stores all of the necessary data for an instance of the unit in my project AS WELL AS its own QWidget unitRibbon which displays some of the data, which I want organised into lists

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

              Then this looks bad. You should decouple the data from their graphic representation aka MVC.

              Since you are already using Qt's related class. You should consider implementing a QAbstractListModel that will wrap your list of BioBloomUnit and use a QListView to show them.

              By the way, what is ribbonAddress for a widget ?

              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
              1
              • H Offline
                H Offline
                Hubbard
                wrote on last edited by
                #9

                ribbonAddress is a pointer to a UnitRibbon class and is a member in BioBloomUnit, its a widget with its own UI file, BioBloomUnit is NOT a widget and is a normal QObject class, I was to display all of the UnitRibbons for each unit in a list.

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

                  That I understood, hence my suggestion for a separation of concerns. BioBloomUnit should not care about the GUI that should show the data it represents.

                  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
                  1
                  • H Offline
                    H Offline
                    Hubbard
                    wrote on last edited by
                    #11

                    Would it be better if the UnitRibbons exist in the mainWindow and instead take BioBloomUnit as an argument (either by constructor or member method) to get their data?

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      Hubbard
                      wrote on last edited by
                      #12

                      so I updated my code to the following

                      UnitRibbon* exampleRibbonAddress = new UnitRibbon;
                      QListWidgetItem* exampleItemAddress = new QListWidgetItem(ui->UnitList);
                      //exampleItemAddress->setSizeHint(exampleRibbonAddress->sizeHint());
                      ui->UnitList->setItemWidget(exampleItemAddress, exampleRibbonAddress);
                      ui->UnitList->addItem(exampleItemAddress);
                      

                      Now it only shows the top half of the widget, when the sizeHint is uncommented it becomes invisible entirely

                      mrjjM 1 Reply Last reply
                      0
                      • H Hubbard

                        so I updated my code to the following

                        UnitRibbon* exampleRibbonAddress = new UnitRibbon;
                        QListWidgetItem* exampleItemAddress = new QListWidgetItem(ui->UnitList);
                        //exampleItemAddress->setSizeHint(exampleRibbonAddress->sizeHint());
                        ui->UnitList->setItemWidget(exampleItemAddress, exampleRibbonAddress);
                        ui->UnitList->addItem(exampleItemAddress);
                        

                        Now it only shows the top half of the widget, when the sizeHint is uncommented it becomes invisible entirely

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @Hubbard
                        Hi
                        Setting size hint is good idea.
                        It will use that for widget size when placed on "row"

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

                          Might be a silly question but are you sure your properly set the layouts on your widgets ? Having to call sizeHint like that is surprising.

                          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
                          1
                          • H Offline
                            H Offline
                            Hubbard
                            wrote on last edited by
                            #15

                            I have removed the graphical widget from my class and made it a separate class and now it is displaying, I'm surprised it works like that, however I've run into a different yet related issue:-

                            exampleUnitAddress = new BioBloomUnit(this);        //instance example
                            
                            setupOtherWindows();
                            setupPushButtons();
                            
                            UnitRibbon* exampleRibbonAddress = new UnitRibbon;
                            QListWidgetItem* exampleItemAddress = new QListWidgetItem(ui->UnitList);
                            exampleItemAddress->setSizeHint(exampleRibbonAddress->size());
                            ui->UnitList->setItemWidget(exampleItemAddress, exampleRibbonAddress);
                            ui->UnitList->addItem(exampleItemAddress);
                            
                            //UnitRibbonPressSlot() calls BioBloomUnit::UnitWindow->show()
                            
                            connect(ui->ExampleButton, SIGNAL(released()), exampleUnitAddress, SLOT(unitRibbonPressSlot()));
                            
                            connect(ui->UnitList, SIGNAL(itemClicked(exampleItemAddress)), exampleUnitAddress, SLOT(unitRibbonPressSlot()) );
                            

                            ExampleButton (a QPushButton on the ui) successfully opens the unit's window, however pressing on the Ribbon for the example Unit does not.

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              Hi
                              When using the old SLOT and SIGNAl macros, you should always
                              check the return value of connect.
                              Also you seem to mix signatures/odd syntax
                              connect(ui->UnitList, SIGNAL(itemClicked(exampleItemAddress)), exampleUnitAddress, SLOT(unitRibbonPressSlot()) );
                              What is exampleItemAddress ?
                              and unitRibbonPressSlot should also accept such paramter
                              Are you sure that UnitLists signal itemClicked have that signature ?

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                Hubbard
                                wrote on last edited by
                                #17

                                void itemClicked(QListWidgetItem *item)

                                can you explain your response more please?

                                It takes in the item instanced above (and shown in the list) and has no return

                                1 Reply Last reply
                                0
                                • H Offline
                                  H Offline
                                  Hubbard
                                  wrote on last edited by
                                  #18

                                  What do you mean by the 'old' signal and slot macros?

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    Hubbard
                                    wrote on last edited by
                                    #19

                                    Ah I see, the signals are only allowed to be passed parameter types, not names, so how do I link the specific item clicked on to it's specific unit's window?

                                    mrjjM 1 Reply Last reply
                                    0
                                    • H Hubbard

                                      Ah I see, the signals are only allowed to be passed parameter types, not names, so how do I link the specific item clicked on to it's specific unit's window?

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by mrjj
                                      #20

                                      @Hubbard
                                      Hi
                                      Yes for connect, you only list the type and not the parameter name.

                                      The actual parameter is added by the widget that emit the signal.
                                      ( in this case when you click something)

                                      Normally, the SLOT should accept the same paramter(s)

                                      by old i mean
                                      https://wiki.qt.io/New_Signal_Slot_Syntax
                                      Its possible to use other syntax that fails at compile time.
                                      The price is a ugly syntax if there are overloaded signals ( one send index, other send string as example)

                                      H 1 Reply Last reply
                                      1
                                      • mrjjM mrjj

                                        @Hubbard
                                        Hi
                                        Yes for connect, you only list the type and not the parameter name.

                                        The actual parameter is added by the widget that emit the signal.
                                        ( in this case when you click something)

                                        Normally, the SLOT should accept the same paramter(s)

                                        by old i mean
                                        https://wiki.qt.io/New_Signal_Slot_Syntax
                                        Its possible to use other syntax that fails at compile time.
                                        The price is a ugly syntax if there are overloaded signals ( one send index, other send string as example)

                                        H Offline
                                        H Offline
                                        Hubbard
                                        wrote on last edited by
                                        #21

                                        @mrjj
                                        Could you explain what you meant about the actual parameter is passed by the item that's emits the signal please

                                        How could I use this to open a different window for a different item?

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • H Hubbard

                                          @mrjj
                                          Could you explain what you meant about the actual parameter is passed by the item that's emits the signal please

                                          How could I use this to open a different window for a different item?

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by jsulm
                                          #22

                                          @Hubbard Please read http://doc.qt.io/qt-5/signalsandslots.html
                                          Parameters are not passed at the time you connect a slot to a signal (how would it be possible?), but at the time the signal is emitted. So, the object emitting the signal passes the parameter.
                                          For example lets say you have a class with void someSignal(int someParameter) signal, then an instance of this class would emit the signal like this:

                                          emit someSignal(10);
                                          

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          2

                                          • Login

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