PDA

View Full Version : Fill QTableWidget and refresh it with button



Doppelganger
18th December 2014, 09:33
Hi all!
Guys please help me. I have QTableWidget in Dialog Window

AddAgentDlg::AddAgentDlg(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

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);
ui.tableWidget->setColumnCount(5);
ui.tableWidget->setRowCount(dirContent.size());
ui.tableWidget->resizeRowsToContents();
ui.tableWidget->resizeColumnsToContents();
QTableWidgetItem* 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 );
ui.tableWidget->setItem(row,column++,img);
if (column > columnNumber) {
column = 0;
row++;
}
}

}
Is it right? How can I make it update via pushButton? ui.tableWidget->update() nor repaint doesn't work.

anda_skoa
18th December 2014, 13:28
Where to you update the content?

And check the output log of your application, it will tell you that your fourth connect is not possible.

Cheers,
_

Doppelganger
18th December 2014, 13:38
I have push button that connect with this slot

ui.tableWidget->clear();
ui.tableWidget->setRowCount(0);
ui.tableWidget->setColumnCount(0);
filltable();
Where filltable is function to populate QTableWidget. First populate in window is perfect. But when I push button, clear data and try repopulate table this is what i get : First populate - ok 10825 after press pushButton 10826 it's shift images. i don't understand why

anda_skoa
18th December 2014, 16:05
So both times you call filltable()?

The code you posted so far has nothing to do with filltable()

How do you expect help if your program is doing something totally different than what you post?

Cheers,
_