当前位置:网站首页>Qtablewidget usage explanation
Qtablewidget usage explanation
2022-04-23 17:56:00 【liu_ jie_ bin】
QTableWidget brief introduction
QTableWidget Class provides an item based table view with a default model .Table Widgets provide standard display tools for applications .QTableWidget Items in by QTableWidgetItem Provide .
effect
.h file
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_btnAdd_clicked();
void on_btnClear_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
.ccp file
#include "widget.h"
#include "ui_widget.h"
#include<QSpinBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->comboBox->addItem(QStringLiteral(" male "));
ui->comboBox->addItem(QStringLiteral(" Woman "));
// Set number of columns
ui->tableWidget->setColumnCount(3);
// Set the number of lines
//ui->tableWidget->setRowCount(15);
// Set the vertical header invisible
ui->tableWidget->verticalHeader()->setVisible(false);
// For header title QStringList To express
QStringList headerText;
headerText<<QStringLiteral(" name ")<<QStringLiteral(" Gender ")<<QStringLiteral(" Age ");
ui->tableWidget->setHorizontalHeaderLabels(headerText);
// Settings are not editable
ui->tableWidget->setEditTriggers(QTableWidget::NoEditTriggers);
// Set the selected full line mode
ui->tableWidget->setSelectionBehavior(QTableWidget::SelectRows);
// Set radio mode
ui->tableWidget->setSelectionMode(QTableWidget::SingleSelection);
// Turn on alternate line background color
ui->tableWidget->setAlternatingRowColors(true);
// Set content adaptive width
//ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
// Set the adaptive length of the last column
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
// Set header style
ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{padding:3px; margin:0px; color:#DCDCDC; border:1px solid #242424; \ border-left-width:0px; border-right-width:1px; border-top-width:0px; border-bottom-width:1px; \ background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #646464,stop:1 #525252);}");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btnAdd_clicked()
{
// Get the number of lines
int rowCount = ui->tableWidget->rowCount();
// Insert row
ui->tableWidget->insertRow(rowCount);
// Add child QSpinBox
QSpinBox *ages = new QSpinBox();
ages->setValue(24);
ui->tableWidget->setCellWidget(rowCount,2, ages);
QString strName = ui->lineEdit->text();
// Add name child
QTableWidgetItem *nameItem = new QTableWidgetItem(strName);
// Set individual item attribute
nameItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QFont font = nameItem->font();
font.setBold(true);
nameItem->setTextColor(QColor(0,0,255));
// add to
ui->tableWidget->setItem(rowCount,0,nameItem);
// Add sex sub item
QString strSex = ui->comboBox->currentText();
// Set individual item attribute
QTableWidgetItem *sexItem = new QTableWidgetItem(strSex);
sexItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
sexItem->setTextColor(QColor(255,0,0));
// add to
ui->tableWidget->setItem(rowCount,1,sexItem);
}
void Widget::on_btnClear_clicked()
{
ui->tableWidget->clearContents();
ui->tableWidget->setRowCount(0);
}
ui Layout
版权声明
本文为[liu_ jie_ bin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231752142884.html
边栏推荐
- Encapsulate a timestamp to date method on string prototype
- 440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- Leak detection and vacancy filling (6)
- ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
- 31. Next arrangement
- cv_ Solution of mismatch between bridge and opencv
- Sword finger offer 03 Duplicate number in array
- 386. Dictionary order (medium) - iteration - full arrangement
- Cloud native Virtualization: building edge computing instances based on kubevirt
猜你喜欢
Chrome浏览器的跨域设置----包含新老版本两种设置
2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
The ultimate experience, the audio and video technology behind the tiktok
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
SystemVerilog (VI) - variable
编译原理 求first集 follow集 select集预测分析表 判断符号串是否符合文法定义(有源码!!!)
Dry goods | how to extract thumbnails quickly?
cv_ Solution of mismatch between bridge and opencv
Comparison between xtask and kotlin coroutine
470. Rand10() is implemented with rand7()
随机推荐
Halo open source project learning (II): entity classes and data tables
198. Looting - Dynamic Planning
C# 的数据流加密与解密
EasymodbusTCP之clientexample解析
470. Rand10() is implemented with rand7()
开源按键组件Multi_Button的使用,含测试工程
k8s之实现redis一主多从动态扩缩容
Welcome to the markdown editor
2022年茶艺师(初级)考试模拟100题及模拟考试
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
Remember using Ali Font Icon Library for the first time
2021 Great Wall Cup WP
Element calculation distance and event object
cartographer_ There is no problem compiling node, but running the bug that hangs directly
Thirteen documents in software engineering
Anchor location - how to set the distance between the anchor and the top of the page. The anchor is located and offset from the top
Implementation of k8s redis one master multi slave dynamic capacity expansion
Arithmetic expression
cv_ Solution of mismatch between bridge and opencv
ES6 new method