谁能给我一个提示,告诉我如何在 QTreeWidget 的标题中放置一个按钮 一个最小的例子非常受欢迎?
问问题
5810 次
2 回答
3
对于评论中的人(基本上任何其他可能会因此而停下来的人),这里是以下代码的翻译 python 版本:
from PySide2 import QtWidgets, QtCore
import sys
class Header(QtWidgets.QHeaderView):
def __init__(self, orientation, parent=None):
super(Header, self).__init__(orientation, parent)
self.button = QtWidgets.QPushButton('Button text', self)
class TreeWidget(QtWidgets.QTreeWidget):
def __init__(self, parent=None):
super(TreeWidget, self).__init__(parent)
item0 = QtWidgets.QTreeWidgetItem(["Item 0"])
item1 = QtWidgets.QTreeWidgetItem(["Item 1"])
self.addTopLevelItem(item0)
self.addTopLevelItem(item1)
self.createHeader()
def createHeader(self):
header = Header(QtCore.Qt.Horizontal, self)
self.setHeader(header)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = TreeWidget()
w.show()
sys.exit(app.exec_())
#QTreeWidget header is an "stand alone" widget - QHeaderView, so you can set the custom implementation of it.
于 2019-10-06T17:14:29.937 回答
1
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QHeaderView>
#include <QPushButton>
class Header
: public QHeaderView
{
public:
Header(QWidget* parent)
: QHeaderView(Qt::Horizontal, parent)
, m_button(new QPushButton("Button", this))
{
}
private:
QPushButton* m_button;
};
class TreeWidget
: public QTreeWidget
{
Q_OBJECT
public:
TreeWidget()
: QTreeWidget(0)
{
QTreeWidgetItem* item0 = new QTreeWidgetItem(QStringList("Item 0"));
QTreeWidgetItem* item1 = new QTreeWidgetItem(QStringList("Item 1"));
addTopLevelItem(item0);
addTopLevelItem(item1);
createHeader();
}
private:
void createHeader()
{
m_header = new Header(this);
setHeader(m_header);
}
private:
Header* m_header;
};
int main(int c, char** v)
{
QApplication a(c, v);
TreeWidget* tw = new TreeWidget();
tw->show();
return a.exec();
}
//QTreeWidget header is an "stand alone" widget - QHeaderView, so you can set the custom implementation of it.
于 2012-04-16T05:52:21.683 回答