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. Adding custom widget as QListWdgetItem not allowing update of widget member label.
Forum Updated to NodeBB v4.3 + New Features

Adding custom widget as QListWdgetItem not allowing update of widget member label.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.5k 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
    astodolski
    wrote on last edited by
    #1

    I have a custom widget which consists of a label, progressbar, checkbox. On a trigger, I want to add the widget to a QListWidget. The widget appears clipped on the bottom and my slot won't allow updating of the label. In line 13 of MainWindow.cpp, if I don't call new on the custom widget the label updates but only appears once and successive triggers insert only a blank line after the first.

    Controls.h
    @
    #ifndef CONTROLS_H
    #define CONTROLS_H

    #include <QWidget>

    namespace Ui {
    class Controls;
    }

    class Controls : public QWidget
    {
    Q_OBJECT

    public:
    explicit Controls(QWidget *parent = 0);
    ~Controls();

    public slots:
    void WriteInfoNumber(int);

    private:
    Ui::Controls *ui;
    };

    #endif // CONTROLS_H
    @

    Controls.cpp
    @
    #include "controls.h"
    #include "ui_controls.h"

    #include <QDebug>

    Controls::Controls(QWidget *parent) : QWidget(parent), ui(new Ui::Controls)
    {
    ui->setupUi(this);
    }

    Controls::~Controls()
    {
    delete ui;
    }

    void Controls::WriteInfoNumber(int value)
    {
    qDebug() << "ID Value = " << value;
    ui->label->setText(QString::number(value));
    }
    @

    MainWindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "controls.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    signals:
    void UpdateInfo(int);

    private slots:
    void on_actionFile_triggered();

    void on_actionE_xit_triggered();

    private:
    Ui::MainWindow* ui;
    Controls* ctls;
    };

    #endif // MAINWINDOW_H

    @

    MainWindow.cpp
    @
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ctls = new Controls();
    connect(this, SIGNAL(UpdateInfo(int)), ctls, SLOT(WriteInfoNumber(int)));
    }

    void MainWindow::on_actionFile_triggered()
    {
    QTime t = QTime::currentTime();
    QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item, new Controls);
    emit UpdateInfo(t.msecsSinceStartOfDay());
    }
    @

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

      Hi,

      Three things I can see:

      ctls is never shown nor layed out but will have it's value updated (it also is a memory leak since you don't destroy it)

      Your item widget will never receive any values since they are not connected

      If they where connect, they would all show the same value

      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
        astodolski
        wrote on last edited by
        #3

        ctls does show. It's a UI file. It IS layed out as it exists in it's own designer file.

        @
        connect(this, SIGNAL(UpdateInfo(int)), ctls, SLOT(WriteInfoNumber(int)));
        @

        Doesn't the line above connect things?

        Also I fixed the clipping
        @
        item->setSizeHint(QSize(300, 35));
        @

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

          Yes, it connects your MainWindow to ctls and only to ctls

          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
            astodolski
            wrote on last edited by
            #5

            [quote author="SGaist" date="1392758241"]Yes, it connects your MainWindow to ctls and only to ctls[/quote]

            OK, but is that related to only displaying a single row of the widget? Does my sender need to be the QListWidget?

            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