Updating QTableWidget

  • Hi all!
    Guys please help me. I have QTableWidget in Dialog Window
    @AddDlg::AddDlg(QWidget *parent)
    : QDialog(parent)

    connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(pressedB()));
    connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(addtextureclicked()));
    connect(ui.comboBox, SIGNAL(activated(const QString&)), this, SLOT(opendialog(const QString&)));
    connect(ui.tableWidget, SIGNAL(cellDoubleClicked(0,0)), this, SLOT(addtextureclicked()));
    connect(this, SIGNAL(update1()), this, SLOT(update2()));
    attr = ".3ds";

    QDir dir("textures/");
    QFileInfoList dirContent = dir.entryInfoList(QStringList()<< ".Png", QDir::Files | QDir::NoDotAndDotDot);
    addItem = new QTableWidgetItem;
    addItem->setData(Qt::DecorationRole, QPixmap("media/addItem.png").scaled(60,60));
    addItem->setData(Qt::ToolTipRole, QVariant("Add Texture Image"));
    ui.tableWidget->setItem(0,0, addItem);
    Q_FOREACH (QFileInfo fileInfo, dirContent)
    static int row = 0, column = 1, columnNumber = 5;
    QTableWidgetItem* img = new QTableWidgetItem;
    img->setData(Qt::DecorationRole, QPixmap(fileInfo.absoluteFilePath()).scaled(60,60));
    QString str_temp = fileInfo.absoluteFilePath();
    QString str = fileInfo.fileName();
    QVariant str2(str);
    img->setData(Qt::UserRole+1, str2 );
    if (column > columnNumber) {
    column = 0;

    It fill QTableWidget that i made with images from directory. But when I copy new file in this directory I can't refresh tableWidget. Not with ui.tableWidget->update(); nor with ui.tableWidget->repaint(). I'm new with QT so can't figure it out by myself. Where did I mistaker or what am I doing wrong?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Either you would need to call again your loop to re-populate the QTableWidget (don't forget to clean first) or just add your new element at the bottom of your widget.

    Update won't help you since it triggers a repaint of the widget, but that doesn't imply refreshing the underlying data. That's done at the model level which will signal to the view that it must refresh.

  • Thank you for your answer!
    with this
    It does update TableWidget but in weird way first population right after building is ok !http://i.imgur.com/CzIWxUt.png()!, after added new image to directory !http://i.imgur.com/aBEoLd7.png()! It doesnt display one of images and shift it from start column

  • Lifetime Qt Champion

    row is a static variable. as well as column and columnNumber. By the way, why ?

Log in to reply

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