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



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


  • Lifetime Qt Champion

    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



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


  • Lifetime Qt Champion

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



  • [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?


Log in to reply
 

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