PDA

View Full Version : not able to load images to qtablewidget



rashmi
2nd December 2010, 13:01
dear all,

i have couple of questions

1)i have simple program tat loads icons form "images" directory(located in current directory) to 3X3 qtablewidget. code is given below. but when i run the program i cant see any icons on qtablewidget.

2) how do i set the iconsize


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(QPixmap(":/images/*.png")));
t.setItem(r, c, item);
}
}

i have included the snapshot of output i get to make things much clear

qt version= 4.7.0

thanks

regards
rashmi

janorcutt
2nd December 2010, 15:40
try omitting the QPixmap() call you don't need it
eg:

item->setIcon(QIcon(":/images/*.png"));
also you can use the resource browser in designer to make sure your resource path is correct.
I'm assuming that the "*" wildcard is just for the question and not hardwired into your code as this would also cause a problem...

hope that helps :)

rashmi
3rd December 2010, 04:47
item->setIcon(QIcon(":/images/*.png"));

i even tried this but still output remains same.

images directory has 9 images with .png extension i tried loading one particular image example say
item->setIcon(QIcon(":/images/img4.png"));

actually i wanted other way round i wanted all 9 images to fit in to 3X3 table mean to say one image per cell.

can you suggest me where i am going wrong.

ChrisW67
3rd December 2010, 05:41
As janorcutt alluded to, ":/images/*.png" is not the name of an image file so you end up with a null QIcon. You want to load a single image file into each cell. Assuming your files are named img0.png through img8.png, then:


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)
)
);
t.setItem(r, c, item);
}
}

should be closer to what you thought you were doing.

BTW: Your initial screen shot shows a 4x3 grid not a 3x3 grid

rashmi
3rd December 2010, 06:21
hey ChrisW67,

firstly i would like to thank you for your help. and i am sorry as i had mentioned 3X3 instead of 3X4.
i tried doing what you had suggested. but still not able to load images.
i am posting my complete code could you please tell me where i am going wrong. i am not able to make out what is going wrong.

i wanted to mention one more thing is that icon size is 160X110

here is my code. if i am wrong please correct me.
-----------------------------------------------------------------------------------------------------------------

#include <QApplication>
#include <QWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QString>


class TableWidget : public QTableWidget
{

};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

//instance of table

TableWidget t;
// specifies size of tablewidget (window size)

int w=640,h=480;
// specifies column and row count

int row=3,col=4;

//specifies each cell size of table

int cellr,cellc;

t.resize(w,h); //setting window size to width and height specified by user

//sets background color

QPalette palet;
palet.setColor( t.backgroundRole(), Qt::black );
t.setPalette(palet);

//sets selection color i.e, changes color when item is focused.

t.setStyleSheet("selection-background-color:cyan");

//hides headers

t.verticalHeader()->hide();
t.horizontalHeader()->hide();


//calculating cell size based on number of row and column using width and height

cellr=h/row;
cellc=w/col;


t.verticalHeader()->setDefaultSectionSize(cellr);
t.horizontalHeader()->setDefaultSectionSize(cellc);

// set the resize mode to fixed, so the user cannot change the height/width

t.horizontalHeader()->setResizeMode(QHeaderView::Fixed);
t.verticalHeader()->setResizeMode(QHeaderView::Fixed);


t.setRowCount(row);
t.setColumnCount(col);

//sets focus policy;

t.setFocusPolicy(Qt::StrongFocus);



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)));
t.setItem(r, c, item);
}
}
t.show();
return app.exec();
}
---------------------------------------------------------------------------------------------------------------

thanks for help.

ChrisW67
3rd December 2010, 07:10
What doesn't work? The icons load and display in the cells fine here.

The icons are displayed at a fixed size regardless of their original size if you use the default item delegate. If you want an alternate behaviour then you will need to supply a QStyledItemDelegate subclass of your own.

rashmi
3rd December 2010, 07:20
i dont see icons in the output. i can just see 3X4 table with empty cell (black background) as i have included snapshot of output i get in my previous post.

i really cant make out what is going wrong that is the reason i posted my complete code.

any suggestions. please help me.

rashmi
3rd December 2010, 13:06
can any one tell me how do i load images to qtablewidget from resource file.
as i have already mentioned in previous post loading images from directory(code is given in previuos post) is working. so want to try loading from resource to see if that makes difference.

Added after 1 39 minutes:

hey ChrisW67,

thanks alot for your help. i could display icons in window. but icons look really very small in display how do i increase the size of icons to fit cell size. i have included the snapshot have look.

thank you.

please help.

ChrisW67
3rd December 2010, 22:39
You will need to write a custom delegate and attach it to the QTableWidget. The default item delegate renders the item icon as a the small icon you see here and leaves space for the main item content (the Qt::DisplayRole) and optionally the item checkbox as well. Take a look at QStyledItemDelegate and associated examples. You want to provide an alternate paint() method when a valid icon is present.