Results 1 to 20 of 22

Thread: Custom Widget in QListWidget doesn't show correct in list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Custom Widget in QListWidget doesn't show correct in list

    I created a custom widget in Qt designer, (with source for changing properties) consisting of a label, progress bar and a checkbox. I have that as an object in a mainwindow to display in a list when triggered. The first trigger displays the widget. Each successive trigger displays the widget in a subsequent row down the list but there only exists one widget with empty rows above.

    Widget Code:
    Controls.h
    Qt Code:
    1. #ifndef CONTROLS_H
    2. #define CONTROLS_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class Controls;
    8. }
    9.  
    10. class Controls : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Controls(QWidget *parent = 0);
    16. ~Controls();
    17.  
    18. public:
    19. Ui::Controls* ui;
    20.  
    21. public slots:
    22. void WriteInfoNumber(int);
    23.  
    24. private:
    25. // Ui::Controls *ui;
    26. };
    27.  
    28. #endif // CONTROLS_H
    To copy to clipboard, switch view to plain text mode 

    Controls.cpp
    Qt Code:
    1. #include "controls.h"
    2. #include "ui_controls.h"
    3.  
    4. #include <QDebug>
    5.  
    6. Controls::Controls(QWidget *parent) : QWidget(parent), ui(new Ui::Controls)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Controls::~Controls()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Controls::WriteInfoNumber(int value)
    17. {
    18. qDebug() << "ID Value = " << value;
    19. ui->label->setText(QString::number(value));
    20. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "controls.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. signals:
    20. void UpdateInfo(int);
    21.  
    22. private slots:
    23. void on_actionFile_triggered();
    24.  
    25. void on_actionE_xit_triggered();
    26.  
    27. private:
    28. Ui::MainWindow* ui;
    29. Controls* ctls;
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "ui_Controls.h"
    4. #include <QTime>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. ctls = new Controls();
    10. connect(this, SIGNAL(UpdateInfo(int)), ctls, SLOT(WriteInfoNumber(int)));
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::on_actionE_xit_triggered()
    19. {
    20. QApplication::quit();
    21. }
    22.  
    23. void MainWindow::on_actionFile_triggered()
    24. {
    25. QTime t = QTime::currentTime();
    26. QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    27. item->setSizeHint(QSize(300, 35));
    28. ui->listWidget->insertItem(ui->listWidget->size().height(),item);
    29. ui->listWidget->setItemWidget(item, ctls);
    30. emit UpdateInfo(t.msec());
    31. }
    To copy to clipboard, switch view to plain text mode 

    It seems so straightforward. Don't know what I'm missing.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    You only have one object of type Controls, obviously it can only be displayed in one place.

    Cheers,
    _

    P.S.:you don't need to insert the QListWidgetItem, passing the list widget to the constructor takes care of that already.

  3. #3
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    Quote Originally Posted by anda_skoa View Post
    You only have one object of type Controls, obviously it can only be displayed in one place.

    Cheers,
    _

    P.S.:you don't need to insert the QListWidgetItem, passing the list widget to the constructor takes care of that already.
    Yes that's correct. I only want one type of object displayed in the list. I want to add it to the list for every trigger so as to get a list of rows of the object displayed.

    Hope that's clearer than what I originally wrote.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    What anda_skoa said was, "You only have one object of type Controls", not "You only have one type of object." It stands to reason that a single Controls widget can be displayed in only the one place. If you want to simultaneously display multiple widgets of type Controls then you need to create multiple objects of that type.

  5. #5
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    Quote Originally Posted by ChrisW67 View Post
    What anda_skoa said was, "You only have one object of type Controls", not "You only have one type of object." It stands to reason that a single Controls widget can be displayed in only the one place. If you want to simultaneously display multiple widgets of type Controls then you need to create multiple objects of that type.
    .

    Right!

    So call new Controls() in line 29 rather than ctls does the trick for handling multiple entries but breaks the emitted signal for updating the label part of the custom widget. What broke? Perhaps I need a different sender in the signal/slot line in the ctor?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    Quote Originally Posted by astodolski View Post
    .
    So call new Controls() in line 29 rather than ctls does the trick for handling multiple entries but breaks the emitted signal for updating the label part of the custom widget. What broke? Perhaps I need a different sender in the signal/slot line in the ctor?
    How do you connect now?

    Cheers,
    _

  7. #7
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    Quote Originally Posted by anda_skoa View Post
    How do you connect now?

    Cheers,
    _
    Line 10 MainWindow.cpp. I connect the emitted signal to the method WriteInfoNumber in Control.cpp

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    That was from before, when you had only one instance.

    If you want all instances of your widget to receive the signal, you need to connect to each one after its respective creation.

    Cheers,
    _

  9. #9
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    Quote Originally Posted by anda_skoa View Post
    That was from before, when you had only one instance.

    If you want all instances of your widget to receive the signal, you need to connect to each one after its respective creation.

    Cheers,
    _
    So my trigger method is now as follows:

    Qt Code:
    1. void MainWindow::on_actionFile_triggered()
    2. {
    3. QTime t = QTime::currentTime();
    4. QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    5. item->setSizeHint(QSize(300, 35));
    6. ui->listWidget->insertItem(ui->listWidget->size().height(),item);
    7. ui->listWidget->setItemWidget(item, new Controls);
    8. emit UpdateInfo(t.msec());
    9. }
    To copy to clipboard, switch view to plain text mode 

    The slot connected to UpdateInfo does not change the label. If as you suggest, I connect to each successive instance, I still don't get a change in the label from the slot associated. What I do see however is a call to the slot for each entry in the list: i.e 1 call for the first, 2 calls for the second entry, 3 calls for the third in the list etc.

    Should there be a different connect?

    What I was able to do as perhaps an interitm step is to call the following which fortunately sets the text for the label:
    Qt Code:
    1. item->setText(QString::number(t.msec()));
    To copy to clipboard, switch view to plain text mode 

    Since I have two other controls, that being a progress bar and the other a checkbox, I still am not sure how to set those other items in the custom widget
    Last edited by astodolski; 19th February 2014 at 14:50.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    Quote Originally Posted by astodolski View Post
    So my trigger method is now as follows:

    Qt Code:
    1. void MainWindow::on_actionFile_triggered()
    2. {
    3. QTime t = QTime::currentTime();
    4. QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    5. item->setSizeHint(QSize(300, 35));
    6. ui->listWidget->insertItem(ui->listWidget->size().height(),item);
    7. ui->listWidget->setItemWidget(item, new Controls);
    8. emit UpdateInfo(t.msec());
    9. }
    To copy to clipboard, switch view to plain text mode 
    That is still missing the connect

    Qt Code:
    1. void MainWindow::on_actionFile_triggered()
    2. {
    3. QTime t = QTime::currentTime();
    4. QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    5. item->setSizeHint(QSize(300, 35));
    6.  
    7. // create instance of Controls
    8. Controls *ctls = new Controls;
    9.  
    10. // connect signal to Controls instance
    11. connect(this, SIGNAL(UpdateInfo(int)), ctls, SLOT(WriteInfoNumber(int)));
    12.  
    13. ui->listWidget->setItemWidget(item, ctls);
    14. emit UpdateInfo(t.msec());
    15. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. #11
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Custom Widget in QListWidget doesn't show correct in list

    Quote Originally Posted by anda_skoa View Post
    That is still missing the connect

    Qt Code:
    1. void MainWindow::on_actionFile_triggered()
    2. {
    3. QTime t = QTime::currentTime();
    4. QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    5. item->setSizeHint(QSize(300, 35));
    6.  
    7. // create instance of Controls
    8. Controls *ctls = new Controls;
    9.  
    10. // connect signal to Controls instance
    11. connect(this, SIGNAL(UpdateInfo(int)), ctls, SLOT(WriteInfoNumber(int)));
    12.  
    13. ui->listWidget->setItemWidget(item, ctls);
    14. emit UpdateInfo(t.msec());
    15. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    I may be misunderstanding. The Controls instance is the object named ctls. The signal UpdateInfo is connected to the WriteInfoNumber slot belonging to the Controls class.

Similar Threads

  1. Replies: 4
    Last Post: 18th October 2013, 17:15
  2. Replies: 8
    Last Post: 21st May 2010, 11:26
  3. QListWidget doesn't show icons
    By franco.amato in forum Qt Programming
    Replies: 8
    Last Post: 16th March 2010, 14:15
  4. Custom Widget doesn t show.
    By Frej in forum Qt Tools
    Replies: 12
    Last Post: 11th March 2010, 10:48
  5. QMainWindow and custom widget doesn't show
    By Peppy in forum Qt Programming
    Replies: 9
    Last Post: 26th December 2009, 15:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.