English 中文(简体)
how do i place QTableWidgetItem Icon in center of cell
原标题:
  • 时间:2009-11-10 13:27:46
  •  标签:
  • qt
  • qt4
  • pyqt4

i want a table cell to have just an icon without any text.

i see the QTableWidgetItem class has a method to align the text (int QTableWidgetItem::textAlignment () const)

i find no way to adjust the placement of the icon (which seems to get stuck on the left -- even where there is no text in the cell)

look at the Status and Energy columns.

alt text

最佳回答

You can affect the position of the icon in relation to the text via the style options.

If the QTableWidgetItem is constructed without any text (via the constructor that does not accept a text argument), then the Qt::DisplayRole data item is not set and the text will not be displayed nor will it affect the icons display rectangle.

I was able to affect the position of the QTableWidgetItem s icon by subclassing the QTableWidget, overriding the viewOptions method and setting the decorationAlignment field of the view options, like this:

QStyleOptionViewItem MyTableWidget::viewOptions() const
{
    QStyleOptionViewItem option = QTableWidget::viewOptions();
    option.decorationAlignment = Qt::AlignHCenter | Qt::AlignCenter;
    option.decorationPosition = QStyleOptionViewItem::Top;
    ...
    return option;
}
问题回答

I had a similar problem I solved it without sub classing by using a QLabel as cellwidget
(sadly i needed to use a layout, too):

int row = 0;
int column = 0;
QSize sizeIcon(32, 32);
QString iconSrc = ":/Actions/myicon.png";

QWidget *pWidget = new QWidget();
QLabel *label = new QLabel;
label->setMaximumSize(sizeIcon);
label->setScaledContents(true);
label->setPixmap(QPixmap(iconSrc));
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(label);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pWidget->setLayout(pLayout);

this->ui->myTableWidget->setCellWidget(row, column, pWidget);

I used the following approach:
http://falsinsoft.blogspot.de/2013/11/qtablewidget-center-checkbox-inside-cell.html

You can use: setCellWidget, as follow:

QLabel *lbl_item = new QLabel();
lbl_item ->setPixmap(*ui->my_label->pixmap());
lbl_item ->setAlignment(Qt::AlignHCenter);
ui->my_tablewidget->setCellWidget(row, column, lbl_item);

This will put the Icon at the center

I think it s the basic behavior of the QTableWidgetItem that doesn t allow you to change anything related to the icon...

Have a look at the example "Star Delegate Example" from Qt, maybe you ll find something interesting for your problem ! It s a little bit of job, but you should be able to "draw" the cell in a different way when it does not contain any text !

Hope this help a bit!

It seems you must subclass QTableWidgetItem and rewrite the paintEvent function where you will be able to draw icon where you want.





相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签