其他分享
首页 > 其他分享> > Qt开发经验小技巧186-190

Qt开发经验小技巧186-190

作者:互联网

186.关于是使用QList还是QVector的问题,一直是众多Qter的选择问题,主要是这两个玩意提供的的接口函数基本一致,比如插入、删除、取值等。

  1. 关于mouseTracking鼠标追踪和tabletTracking平板追踪的几点官方说明。
  1. 关于QTableWidget等控件调用自带的removeRow、clearContents、clear函数删除了里面的item和内容,会自动调用item或者cellwidget的析构函数进行资源释放,不用自己手动再去释放。
//每次调用 clearContents 都会自动清理之前的item
ui->tableWidget->clearContents();
for (int i = 0; i < count; ++i) {
    ui->tableWidget->setItem(i, 0, new QTableWidgetItem("aaa"));
    ui->tableWidget->setItem(i, 1, new QTableWidgetItem("bbb"));
    ui->tableWidget->setCellWidget(i, 2, new QPushButton("ccc"));
}

  1. 对于QListView(QListWidget)、QTreeView(QTreeWidget)、QTableView(QTableWidget)这种类型的控件,可以通过setChecked来让对应的item产生复选框效果,很多人(包括曾经的自己)误以为这就是复选框控件,其实不是的,他是对应控件的indicator指示器,所以想要更换样式,不能说设置了QCheckBox的样式就有效果,而要单独对齐indicator指示器设置样式才行。
QCheckBox::indicator,QGroupBox::indicator,QTreeWidget::indicator,QListWidget::indicator{
width:13px;
height:13px;
}

QCheckBox::indicator:unchecked,QGroupBox::indicator:unchecked,QTreeWidget::indicator:unchecked,QListWidget::indicator:unchecked{
image:url(:/qss/flatwhite/checkbox_unchecked.png);
}

QCheckBox::indicator:unchecked:disabled,QGroupBox::indicator:unchecked:disabled,QTreeWidget::indicator:unchecked:disabled,QListWidget::indicator:disabled{
image:url(:/qss/flatwhite/checkbox_unchecked_disable.png);
}

QCheckBox::indicator:checked,QGroupBox::indicator:checked,QTreeWidget::indicator:checked,QListWidget::indicator:checked{
image:url(:/qss/flatwhite/checkbox_checked.png);
}

QCheckBox::indicator:checked:disabled,QGroupBox::indicator:checked:disabled,QTreeWidget::indicator:checked:disabled,QListWidget::indicator:checked:disabled{
image:url(:/qss/flatwhite/checkbox_checked_disable.png);
}

QCheckBox::indicator:indeterminate,QGroupBox::indicator:indeterminate,QTreeWidget::indicator:indeterminate,QListWidget::indicator:indeterminate{
image:url(:/qss/flatwhite/checkbox_parcial.png);
}

QCheckBox::indicator:indeterminate:disabled,QGroupBox::indicator:indeterminate:disabled,QTreeWidget::indicator:indeterminate:disabled,QListWidget::indicator:indeterminate:disabled{
image:url(:/qss/flatwhite/checkbox_parcial_disable.png);
}

  1. 关于QTableView(采用model数据源)、QTableWidget列名列宽设置,有时候发现没有起作用,原来是对代码设置的顺序有要求,比如setColumnWidth前必须先setColumnCount,不然列数都没有,哪来的列宽,包括setHorizontalHeaderLabels设置列标题集合也是,前提都要先有列。
void frmSimple::initForm()
{
    row = 100;
    column = 6;
    model = new QStandardItemModel(this);

    columnNames << "列标题1" << "列标题2" << "列标题3" << "列标题4" << "列标题5" << "列标题6";
    columnWidths << 100 << 100 << 100 << 100 << 100 << 100;
}

void frmSimple::initTableView()
{
    //清空数据
    model->clear();
    //先设置数据模型,否则 setColumnWidth 不起作用
    ui->tableView->setModel(model);

    //设置列数及列标题和列宽
    model->setColumnCount(column);
    for (int i = 0; i < column; ++i) {
        model->setHeaderData(i, Qt::Horizontal, columnNames.at(i));
        ui->tableView->setColumnWidth(i, columnWidths.at(i));
    }

    //循环添加行数据
    for (int i = 0; i < row; ++i) {
        //循环添加一行的列
        QList<QStandardItem *> items;
        for (int j = 0; j < column; ++j) {
            QStandardItem *item = new QStandardItem;
            //最后一列显示时间区别开来
            if (j == column - 1) {
                QDateTime now = QDateTime::currentDateTime().addSecs(i);
                item->setText(now.toString("yyyy-MM-dd HH:mm:ss"));
            } else {
                item->setText(QString("行%1_列%2").arg(i + 1).arg(j + 1));
            }
            items << item;
        }
        model->appendRow(items);
    }
}

void frmSimple::initTableWidget()
{
    //清空数据
    ui->tableWidget->clearContents();

    //设置列标题和列数及列宽
    ui->tableWidget->setColumnCount(column);
    ui->tableWidget->setHorizontalHeaderLabels(columnNames);
    for (int i = 0; i < column; ++i) {
        ui->tableWidget->setColumnWidth(i, columnWidths.at(i));
    }

    //添加数据
    ui->tableWidget->setRowCount(row);
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < column; ++j) {
            QTableWidgetItem *item = new QTableWidgetItem;
            //最后一列显示时间区别开来
            if (j == column - 1) {
                QDateTime now = QDateTime::currentDateTime().addSecs(i);
                item->setText(now.toString("yyyy-MM-dd HH:mm:ss"));
            } else {
                item->setText(QString("行%1_列%2").arg(i + 1).arg(j + 1));
            }
            ui->tableWidget->setItem(i, j, item);
        }
    }
}

标签:indicator,checked,Qt,190,disabled,item,tableWidget,ui,186
来源: https://blog.csdn.net/qq_45662588/article/details/121628588