Qt之QTableWidget 表头添加QComBox
Qt 添加 表头 QTableWidget
2023-09-14 09:07:01 时间
代码之路
代码很简单,重写QHeaderView类,然后设置到对应的Table中即可。
重写QHeaderView类
class CheckBoxHeaderView : public QHeaderView
{
Q_OBJECT
public:
CheckBoxHeaderView(int checkColumnIndex,
Qt::Orientation orientation,
QWidget * parent = 0) :
QHeaderView(orientation, parent)
{
// 默认ComboBox;
m_comboBox = new QComboBox(this);
m_comboBox->addItems(QStringList() << "123" << "456" << "789");
}
// 获取当前comboBox文字;
QString getCurrentComboBoxText()
{
return m_comboBox->currentText();
}
// 设置自定义ComboBox;
void setComboBoxObject(QComboBox* object)
{
m_comboBox = object;
m_comboBox->setParent(this);
}
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
if (logicalIndex == 0)
{
m_comboBox->setGeometry(rect);
}
else
{
QHeaderView::paintSection(painter, rect, logicalIndex);
}
}
private:
QComboBox * m_comboBox;
};
简单测试
class MyTableWidgetWidthComboBox : public QTableWidget
{
Q_OBJECT
public:
MyTableWidgetWidthComboBox(QWidget *parent = Q_NULLPTR);
private:
CheckBoxHeaderView * m_checkBoxHeaderView;
};
MyTableWidgetWidthComboBox::MyTableWidgetWidthComboBox(QWidget *parent)
: QTableWidget(parent)
{
this->setAlternatingRowColors(true);
this->setColumnCount(3);
this->setSelectionMode(QAbstractItemView::SingleSelection);
this->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->setSelectionBehavior(QAbstractItemView::SelectRows);
// 自定义表头
m_checkBoxHeaderView = new CheckBoxHeaderView(0, Qt::Horizontal, this);
// 自定义ComboBox;
QComboBox* comboBox = new QComboBox;
comboBox->addItems(QStringList() << "abc" << "def" << "789");
m_checkBoxHeaderView->setComboBoxObject(comboBox);
// 设置表头;
this->setHorizontalHeader(m_checkBoxHeaderView);
this->setHorizontalHeaderLabels(QStringList() << "1" << "2" << "3");
}
效果图
相关文章
- Qt高并发
- qt集成ziplib压缩文件和目录「建议收藏」
- Qt截图软件2-实现截全屏功能
- C/C++ Qt StatusBar 底部状态栏应用
- C/C++ Qt TreeWidget 单层树形组件应用
- QT软件开发-基于FFMPEG设计视频播放器-软解图像(一)
- PyQt5安装以及使用教程 (window 上含QT Designer的安装)
- Qt官方示例-迷你网页浏览器
- Qt Qml 开发超高清 4K、8K 视频直播视频客户端
- 聊聊QT添加MySQL驱动依赖的问题
- Linux下Qt安装指南:快速部署Qt运行环境(linux下qt安装教程)
- Qt在Linux上的移植实践(qt移植到linux)
- 使用Qt快速设置Linux时间(qt设置linux时间)
- Linux安装Qt:一步一步指南(linux安装qt)
- Linux 环境下运用 Qt 编程的秘籍(linux下的qt编程)
- Linux 下 Qt 调试技巧(qt调试linux)
- Linux虚拟机中安装Qt的指南(linux虚拟机安装qt)
- 跨平台应用使用Qt开发Linux跨平台应用(qt开发linux)
- Linux下的Qt开发编程实践(linux版qt)
- 开发Linux下 Qt开发:从入门到精通.(linux下的qt)
- Qt连接Oracle数据库的驱动问题(qtoracle驱动)
- 如何在Linux下编译Qt?(linux下编译qt)
- Linux Qt视觉学习:学会系统开发的天才秘诀(linuxqt视频教程)
- 崭新的体验——Qt在Linux的革新之旅(qtlinuxso)
- 使用QT操作MySQL数据库(qt的mysql数据库)
- Linux环境下如何卸载Qt应用(linux卸载qt)
- Linux Qt窗口:快速构建你的GUI界面(linux qt 窗口)
- 深入浅出:使用Qt连接MSSQL(qt 连接mssql)
- 在Linux下体验Qt的简便安装之旅(在linux下安装qt)
- Linux下Qt音频播放:轻松享受音乐乐活力(linux下qt音频播放)
- 利用Qt和MySQL构建App:快速下载详情(qtmysql下载)
- Qt Creator 5.0 RC 发布