我想在 QTree 中使用 QTable 设计一个特定的布局。请参阅以下线框图:
知道数量后是否可以做到这一点:
对话框 1 下的第一个节点分为 3 个(也可能是 4 个)。时间,后面是头像图标,后面是带有文本的蓝色气泡,最后是数字(在我的例子中,75)那么,第一个问题,是否可以将树节点分成四个?
第2部分由叶子节点组成,这些节点是QTable。那么,问题2,是否可以创建嵌入QTree内的QTable?
因此,这需要一些时间才能使其可用,以便将数据放入树和后续表中,但这是一个起点。
class tcTreeTableWidget : public QTableWidget
{
Q_OBJECT
public:
tcTreeTableWidget() : QTableWidget()
{
setColumnCount (3);
QStringList headers;
headers << "Table Column 1" << "Table Column 2" << "Table Column 3";
setHorizontalHeaderLabels (headers);
for (int row = 0; row < 8; ++row)
{
for (int col = 0; col < 3; ++col)
{
setRowCount (row + 1);
setItem (row, col, new QTableWidgetItem (QString("R %1, C %2").arg(row).arg(col)));
}
}
}
};
class tcTreeTableWidgetItem : public QTreeWidgetItem
{
public:
tcTreeTableWidgetItem (QTreeWidgetItem* p) : QTreeWidgetItem (p)
{
setFirstColumnSpanned (true);
m_tree_widget = new tcTreeTableWidget();
treeWidget()->setItemWidget(this, 0, m_tree_widget);
}
private:
tcTreeTableWidget* m_tree_widget;
};
class tcMyWidget : public QWidget
{
Q_OBJECT
public:
tcMyWidget (QWidget* p);
protected:
void setupUI ();
QTreeWidget* m_tree;
};
tcMyWidget::tcMyWidget (QWidget* p)
: QWidget (p)
{
setupUI();
}
void tcMyWidget::setupUI()
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget (m_tree = new QTreeWidget (this));
m_tree->setColumnCount (4);
QStringList headers;
headers << "Tree Column 1" << "Tree Column 2" << "Tree Column 3" << "Tree Column 4";
m_tree->setHeaderLabels (headers);
for (int i = 0; i < 5; ++i)
{
QTreeWidgetItem *pitem;
pitem = new QTreeWidgetItem (m_tree);
for (int j = 0; j < 4; ++j)
{
pitem->setText (j, QString("R %1, C %2").arg(i).arg(j));
}
tcTreeTableWidgetItem* ptitem = new tcTreeTableWidgetItem (pitem);
}
}