[SOLVED]Picture in center of QTableWidget's cell
-
Hello. I have QTableWidget. I want to insert picture (without text) in the cell of QTableWidget. Picture must be in the center of cell. When I trying this:
@
QTableWidget *sampleTableWIdget = new QTableWidget;
...
QTableWidgetItem *tableItemSmthing = new QTableWidgetItem(QIcon(":/images/icon_sample.png"),"");
sampleTableWIdget->setItem(1,1, tableItemSmthing);
@
But icon appears in the left side of cell (1,1).
I know method to center widgets in the cell (for ex. QCheckBox):
@
QTableWidget sampleTableWIdget = new QTableWidget;
...
QWidget wdg = new QWidget;
QCheckBox checkBox = new QCheckBox(wdg);
QHBoxLayout layout = new QHBoxLayout(wdg);
layout->setContentsMargins(0,0,0,0);
layout->addWidget(checkBox);
layout->setAlignment( Qt::AlignCenter );
wdg->setLayout(layout);
sampleTableWIdget->setCellWidget(1,1, wdg);
@but this method didn't work for icons or pixmaps. Can anyone help me?
-
Problem was solved by using something like this:
icon.h@
class Icon : public QWidget
{
Q_OBJECT
public:
Icon(QWidget* obj=0);
virtual void paintEvent(QPaintEvent*);
@icon.cpp
@
Icon::Icon(QWidget* obj): QWidget(obj) {}void Icon::paintEvent(QPaintEvent*)
{
QPainter p(this);
QImage img(":/images/icon_sample.png");
int scaleFactor = 18;
QImage tempImg = img.scaled(scaleFactor,scaleFactor, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
p.drawImage(QRectF(width()/2-scaleFactor/2,height()/2-scaleFactor/2,scaleFactor,scaleFactor),tempImg);
}@
mainwindow:
@QTableWidget sampleTableWIdget = new QTableWidget;
....
Icon wdg = new Icon;
sampleTableWIdget->setCellWidget(1, 1, wdg);
@ -
Hi,
Why not just use a QLabel ?
-
[quote author="SGaist" date="1377683487"]Hi,
Why not just use a QLabel ?[/quote]
I din't know about QLabel::setPixmap(..) method, thanks.
problem can be solved by this:
@
QTableWidget *sampleTableWIdget = new QTableWidget;
....
QPixmap pix(":/images/icon_sample.png");
QPixmap resPix = pix.scaled(17,17, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QLabel *lblTest = new QLabel;
lblTest->setPixmap(resPix);
lblTest->setAlignment(Qt::AlignCenter);
sampleTableWIdget->setCellWidget(1, 1, lblTest);
@ -
You're welcome !
And don't forget to update the thread's title prepending solved so other forum users may know a solution has been found :)