[英]Qt Table widget. How to set the meaning/title for vertical header and horizontal header together?
我知道如何將文本標簽設置為行或列標題。 但我想做這樣的事情:
http://i.stack.imgur.com/eMM6U.jpg
我沒有找到關於如何在紅色圓周內做事的任何信息。 我開始相信QTableWidget無法做到這一點。
謝謝 ;)
我認為使用標准頭文件(QHeaderView)是不可能的, 因為 :
注意:每個標頭為每個部分本身呈現數據,而不依賴於委托。 因此,調用標頭的setItemDelegate()函數將不起作用。
所以你需要忘記它並禁用它,你應該實現自己的標題(設置你的顏色,你的文本等),但我當然會幫助意義/標題。 我用下一個項目委托達到了這個目的:
。H:
#ifndef ITEMDELEGATEPAINT_H
#define ITEMDELEGATEPAINT_H
#include <QStyledItemDelegate>
class ItemDelegatePaint : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ItemDelegatePaint(QObject *parent = 0);
ItemDelegatePaint(const QString &txt, QObject *parent = 0);
protected:
void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index ) const;
QSize sizeHint( const QStyleOptionViewItem &option,
const QModelIndex &index ) const;
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget * editor, const QModelIndex & index) const;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;
signals:
public slots:
};
#endif // ITEMDELEGATEPAINT_H
但是所有這些方法在這里都不是很有用,你可以自己用一些特定的動作實現它,我會告訴你main方法 - paint()
void ItemDelegatePaint::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.row() == 0 && index.column() == 0)
{
QRect source1 = QRect(option.rect.topLeft(),option.rect.size()/2);
QRect source2 = QRect(option.rect.topLeft(),option.rect.size()/2);
painter->drawLine(option.rect.topLeft(),option.rect.bottomRight());
source1.moveTopLeft(source1.topLeft() += QPoint(source1.size().width(),0));
painter->drawText(source1,"agent reagent");
source2.moveBottomLeft(source2.bottomLeft() += QPoint(0,source2.size().height()));
painter->drawText(source2,"hallide ion");
}
else
{
QStyledItemDelegate::paint(painter,option,index);
}
}
此代碼顯示主要想法,它不是最終版本,但您應該自己完成所有這些特定的事情。 當然這種方法不是很容易,你可以創建圖片並將其設置為單元格,但在這種情況下圖片將不會很好地擴展。 如果用戶調整某些標題,我的代碼將正常工作。 要證明,請查看不同大小的屏幕截圖。
如果文本元素如此處所示
是足夠的,可以使用標准標題和我在評論中發布的鏈接解決方案:
from PyQt5 import QtCore, QtWidgets
class MyTableWidget(QtWidgets.QTableWidget):
def __init__(self, parent = None):
QtWidgets.QTableWidget.__init__(self, parent)
self.setRowCount(4)
self.setColumnCount(5)
self.items = []
self.items.append(['white ppt','no reaction', 'no reaction', 'no reaction', 'no reaction'])
self.items.append(['no reaction','white ppt', 'dissolves', 'dissolves', 'no reaction'])
self.items.append(['no reaction','pale yellow\nprecipitate', 'dissolves\npartly', 'dissolves', 'no reaction'])
self.items.append(['no reaction','yellow ppt', 'does not\ndissolve', 'does not\ndissolve', 'turns\nblue'])
self.horizontalHeader().setFixedHeight(90)
self.verticalHeader().setFixedWidth(120)
self.hh = ['Ca(NO\u2083)\u2082', 'AgNO\u2083','AgNO\u2083\n+\nNH\u2083','AgNO\u2083\n+\nNa\u2083S\u2082O\u2083','Starch\n+\nNaOCl']
self.vh = ['NaF', 'NaCl', 'NaBr or KBR', 'NaJ']
self.addItems()
self.addHeaderItems()
# text in cornerButton
btnTxt = '{: >15}\n{: >19}\n{:<}\n{:<}'.format('reagent', '\u21D2','halide', 'ion \u21D3')
# add cornerbutton from http://stackoverflow.com/questions/22635867/is-it-possible-to-set-the-text-of-the-qtableview-corner-button
btn = self.findChild(QtWidgets.QAbstractButton)
btn.setText(btnTxt)
btn.installEventFilter(self)
opt = QtWidgets.QStyleOptionHeader()
opt.text = btn.text()
# end cornerbutton
def addItems(self):
for r in range(0,len(self.items)):
for c in range(0,len(self.items[r])):
item = QtWidgets.QTableWidgetItem()
item.setText(self.items[r][c])
self.setItem(r,c,item)
def addHeaderItems(self):
for i in range(0,len(self.hh)):
item = QtWidgets.QTableWidgetItem()
item.setText(self.hh[i])
self.setHorizontalHeaderItem(i,item)
self.setColumnWidth(i,150)
for i in range(0,len(self.vh)):
item = QtWidgets.QTableWidgetItem()
item.setText(self.vh[i])
self.setVerticalHeaderItem(i,item)
self.setRowHeight(i,60)
# eventfilter from http://stackoverflow.com/questions/22635867/is-it-possible-to-set-the-text-of-the-qtableview-corner-button
def eventFilter(self, obj, event):
if event.type() != QtCore.QEvent.Paint or not isinstance(
obj, QtWidgets.QAbstractButton):
return False
# Paint by hand (borrowed from QTableCornerButton)
opt = QtWidgets.QStyleOptionHeader()
opt.initFrom(obj)
styleState = QtWidgets.QStyle.State_None
if obj.isEnabled():
styleState |= QtWidgets.QStyle.State_Enabled
if obj.isActiveWindow():
styleState |= QtWidgets.QStyle.State_Active
if obj.isDown():
styleState |= QtWidgets.QStyle.State_Sunken
opt.state = styleState
opt.rect = obj.rect()
# This line is the only difference to QTableCornerButton
opt.text = obj.text()
opt.position = QtWidgets.QStyleOptionHeader.OnlyOneSection
painter = QtWidgets.QStylePainter(obj)
painter.drawControl(QtWidgets.QStyle.CE_Header, opt)
return True
如果painter.drawControl()
-method被任何其他painter.draw...()
- painter.draw...()
替換為任意元素incl。 可以在cornerButton上繪制圖形。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.