PDA

View Full Version : add text to qtablewidget



rashmi
3rd December 2010, 09:40
hey all,

i have application with QTableWidget of 3X4. i need to load image along with corresponding name of the icon.

i have the following code to display text


QFont fnt;
fnt.setPointSize(30);
fnt.setFamily("Arial");

for (int r = 0; r < t.rowCount(); ++r)
{
for (int c = 0; c < t.columnCount(); ++c)
{
QTableWidgetItem* item = new QTableWidgetItem;
item->setBackgroundColor(Qt::black);
item->setIcon(QIcon(QString(":/images/img%1.png").arg(r * t.columnCount() + c)));

item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);

// writting icon name

item->setTextColor(Qt::white);
item->setFont(fnt);
item->setText("help");
item->setTextAlignment(Qt::AlignBottom|Qt::AlignLeading) ;

t.setItem(r, c, item);
}
}

following code displays help in all the cell. now my question is how would i display different text in each cell based on icon i load.

hope i am clear.

wysota
3rd December 2010, 10:20
Pass different strings to QTableWidgetItem::setText() instead of passing "help" each time.

rashmi
3rd December 2010, 10:56
i hope you did not get what i meant.
i will explain i want to display help in cell1 ,file in cell2 , save in cell3 and exit in cell4 (this completes one row) .similarly i want to display different text in 2 more rows.

hope i am clear now.

wysota
3rd December 2010, 11:03
So what's the problem? What is keeping you from doing that?

rashmi
3rd December 2010, 11:22
it would be helpful if you tel me your suggestion clearly. i really cant understand what are you trying to say.

item->setText("help");

since this is inside for loop it prints help in all the cells of 3X4 table. but i want different text in each cell how do i do it.

wysota
3rd December 2010, 12:56
So why did you place it inside that loop in the first place?

rashmi
3rd December 2010, 13:11
i can place that only inside loop if i place it outside i get error as it is not in scope.

i hope you give me some solution for this.

BalaQT
3rd December 2010, 14:41
hi,

if u want autonumber u can use the following code,


QFont fnt;
fnt.setPointSize(30);
fnt.setFamily("Arial");

for (int r = 0; r < t.rowCount(); ++r)
{
for (int c = 0; c < t.columnCount(); ++c)
{
QTableWidgetItem* item = new QTableWidgetItem;
item->setBackgroundColor(Qt::black);
item->setIcon(QIcon(QString(":/images/img%1.png").arg(r * t.columnCount() + c)));

item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);

// writting icon name

item->setTextColor(Qt::white);
item->setFont(fnt);

item->setText(QString::number(r *c));
item->setTextAlignment(Qt::AlignBottom|Qt::AlignLeading) ;

t.setItem(r, c, item);
}
}


if u want to give a specific name to each item.
then u can use a stringlist to store the name.
and inside the loop u can use that in setText.
ex : setText(myImageName.at(i)); something like tat.

hope it helps
Bala

wysota
3rd December 2010, 16:08
i can place that only inside loop if i place it outside i get error as it is not in scope.
So ask the table widget for the item again outside the inner loop and set the text there.

rashmi
6th December 2010, 05:41
hey bala,

thanks a lot for your help.

regards
rashmi

rashmi
7th December 2010, 10:09
hey bala,

it is not fixed.it may change...........