QTreeWidget中對於其中item的可編輯屬性的設定
看下文件描述先
enum Qt::ItemFlag
flags Qt::ItemFlags
This enum describes the properties of an item:
Constant | Value | Description |
---|---|---|
Qt::NoItemFlags | 0 | It does not have any properties set. |
Qt::ItemIsSelectable | 1 | It can be selected. |
Qt::ItemIsEditable | 2 | It can be edited. |
Qt::ItemIsDragEnabled | 4 | It can be dragged. |
Qt::ItemIsDropEnabled |
8 | It can be used as a drop target. |
Qt::ItemIsUserCheckable | 16 | It can be checked or unchecked by the user. |
Qt::ItemIsEnabled | 32 | The user can interact with the item. |
Qt::ItemIsTristate | 64 | The item is checkable with three separate states. |
Note that checkable items need to be given both a suitable set of flags and an initial state, indicating whether the item is checked or not. This is handled automatically for model/view components, but needs to be explicitly set for instances of
The ItemFlags type is a typedef for QFlags<ItemFlag>. It stores an OR combination of ItemFlag values.
注意最後一句,ItemFlags儲存的是幾個ItemFlag的邏輯或的結果。
所以設定某個Item可編輯時,可以item->setFlags(Qt::ItemIsEditable |Qt::ItemIsEnabled);
QTreeWidget中設定字型顏色
QTreeWidget *A = new QTreeWidget;
QTreeWidgetItem *child=new QTreeWidgetItem(QStringList(tr(portDesAndName.c_str())));
/*藍色表示正在工作*/
child->setForeground(0,QBrush(QColor(Qt::blue)));
A->addChild(child);
通常程式碼格式如下:
ui.treeWidget->setObjectName("treeWidget");
ui.treeWidget->setStyleSheet("QWidget#treeWidget{background-image: url(./pic/netJiedianBg.png);}");
但是,實際情況並不像我們想象的那麼樂觀,例如以下設計好的圖片
我們在實際實現時,如果只使用上面的程式碼後的實際情況是如下所示
可以看到最上面有一條其實還是預設的顏色,這個就需要我們再單獨設定其背景圖片
首先要把那個橫條返回,其實就是Treewidget的Header,然後對返回到Header設定背景即可,方法可能有多種,把Header作為一個Widget使用樣式表加圖片的方法不知道為什麼不行,所以換了種方法,在header裡新增一個label,然後設定其佈局充滿整個HEADER,在label上加圖片。經過試驗是可以的。
程式碼如下:
QHeaderView *headview = ui.treeWidget->header();
QLabel *label = new QLabel("aaa");
label->setPixmap(QPixmap("./pic/netJiedian.png"));
QHBoxLayout *lay = new QHBoxLayout;
lay->setMargin(0);
lay->addWidget(label);
headview->setLayout(lay);
添完後的效果圖如下:
獲得Qtreewidget 上雙擊的那行的資料簡單demo
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->treeWidget->setColumnCount(1);
QTreeWidgetItem *root=new QTreeWidgetItem(ui->treeWidget,QStringList(QString("ROOST")));
QTreeWidgetItem *leaf=new QTreeWidgetItem(root,QStringList(QString("children")));
root->addChild(leaf);
QTreeWidgetItem *leaf1=new QTreeWidgetItem(root,QStringList(QString("children1")));
root->addChild(leaf1);
QTreeWidgetItem *leaf2=new QTreeWidgetItem(root,QStringList(QString("children2")));
root->addChild(leaf2);
QTreeWidgetItem *leaf3=new QTreeWidgetItem(root,QStringList(QString("children3")));
root->addChild(leaf3);
QList<QTreeWidgetItem *>rootList;
rootList<<root;
ui->treeWidget->insertTopLevelItems(0,rootList);
}
在
private slots:
void on_treeWidget_doubleClicked(QModelIndex index);
void MainWindow::on_treeWidget_doubleClicked(QModelIndex index)
{
index=ui->treeWidget->currentIndex();
ui->lineEdit->setText(index.data().toString());
}