QT常规控件操作备忘
QLabel设置边框和颜色:
label->setFrameShape (QFrame::Box);
label->setStyleSheet("border: 1px solid #ff0000");
设置字体大小和颜色:
ui.label是QLabel
ui.label_4->setText("some text");
//设置字号
QFont ft;
ft.setPointSize(12);
ui.label_4->setFont(ft);
//设置颜色
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::red);
ui.label_4->setPalette(pa);
QTreeWidget中加入按钮:
void setItemWidget ( QTreeWidgetItem * item, int column, QWidget * widget );
怎么设置QTreeWidget的列宽?setSizeHint似乎不起作用
QTreeWidgetItem *headItem = watchTree->headerItem();
headItem->setSizeHint(0,QSize(40,20));
headItem->setSizeHint(1,QSize(40,20));
headItem->setText(0,tr("GisRes"));
headItem->setText(1,tr("period"));
是setLineWidth
为了方便,利用QLabel来形成QSplitter之间的间隙,但是无法精确控制宽度
setSizePolicy(QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical)
遍历树QTreeWidget:
int treeCount(QTreeWidget *tree, QTreeWidgetItem *parent = 0)
{
int count = 0;
if (parent == 0) {
int topCount = tree->topLevelItemCount();
for (int i = 0; i < topCount; i++) {
QTreeWidgetItem *item = tree->topLevelItem(i);
if (item->isExpanded()) {
count += treeCount(tree, item);
}
}
count += topCount;
} else {
int childCount = parent->childCount();
for (int i = 0; i < childCount; i++) {
QTreeWidgetItem *item = parent->child(i);
if (item->isExpanded()) {
count += treeCount(tree, item);
}
}
count += childCount;
}
return count;
}
And the usage:
QTreeWidget tw;
// Add items
[..]
int visibleItemsCount = treeCount(&tw);