1.运行结果:

2.代码

main.cpp

 #include "constantdiag.h"
#include <QtWidgets/QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
constantDiag w;
w.show();
return a.exec();
}

constantDialog.h

 #ifndef CONSTANTDIAG_H
#define CONSTANTDIAG_H #include <QtWidgets/QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QStandardItemModel>
#include <QTableView>
#include <QVBoxLayout>
#include <QHeaderView>
#include <QLabel> class ComboDelegate; class constantDiag : public QWidget
{
Q_OBJECT public:
constantDiag(QWidget *parent = );
~constantDiag(); public slots:
void addConstant();
void reduceConstant(); private:
QHBoxLayout *secondHLayout;
QPushButton *btnConstantPlus;
QPushButton *btnConstantMinus;
QStandardItemModel *model;
QTableView *tableview;
QVBoxLayout *wholeLayout;
QHBoxLayout *thirdHLayout;
QPushButton *btnOk;
QPushButton *btnCancel;
QHBoxLayout *topLayout;
QLabel *titleLabel;
QVBoxLayout *wholeVLayout;
ComboDelegate *comboDelegate;
}; #endif // CONSTANTDIAG_H #ifndef COMBODELEGATE_H
#define COMBODELEGATE_H #include <QItemDelegate> class ComboDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboDelegate(QObject *parent = ); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
}; #endif // COMBODELEGATE_H

constantDialog.cpp

 #include "constantdiag.h"

 constantDiag::constantDiag(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(QString::fromLocal8Bit("系统常量"));
resize(,); wholeLayout = new QVBoxLayout(parent); secondHLayout = new QHBoxLayout();
secondHLayout->addStretch(); btnConstantPlus = new QPushButton;
btnConstantPlus->setText("+");
secondHLayout->addWidget(btnConstantPlus); btnConstantMinus = new QPushButton;
btnConstantMinus->setText("-");
secondHLayout->addWidget(btnConstantMinus); secondHLayout->setStretch(,);
secondHLayout->setStretch(,);
secondHLayout->setStretch(,); wholeLayout->addLayout(secondHLayout); model = new QStandardItemModel(,);
model->setHeaderData(,Qt::Horizontal,QString::fromLocal8Bit("名称"));
model->setHeaderData(,Qt::Horizontal,QString::fromLocal8Bit("值"));
model->setHeaderData(,Qt::Horizontal,QString::fromLocal8Bit("单位"));
tableview = new QTableView(parent);
tableview->setModel(model);
tableview->verticalHeader()->setVisible(false);
tableview->horizontalHeader()->setSectionResizeMode(, QHeaderView::Stretch);
tableview->horizontalHeader()->setSectionResizeMode(, QHeaderView::Stretch);
tableview->horizontalHeader()->setSectionResizeMode(, QHeaderView::Stretch);
comboDelegate = new ComboDelegate;
tableview->setItemDelegateForColumn(,comboDelegate);
//tableview->verticalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
//tableview->verticalHeader()->setSectionResizeMode(1,QHeaderView::Stretch); wholeLayout->addWidget(tableview); thirdHLayout = new QHBoxLayout;
thirdHLayout->addStretch(); btnOk = new QPushButton;
btnOk->setText(QString::fromLocal8Bit("确定"));
btnCancel = new QPushButton;
btnCancel->setText(QString::fromLocal8Bit("取消"));
thirdHLayout->addWidget(btnOk);
thirdHLayout->addWidget(btnCancel); thirdHLayout->setStretch(,);
thirdHLayout->setStretch(,);
thirdHLayout->setStretch(,); wholeLayout->addLayout(thirdHLayout);
//wholeLayout->setSizeConstraint(QLayout::SetFixedSize);
//wholeLayout->setStretchFactor(secondHLayout, 1);
//wholeLayout->setStretchFactor(tableview, 2);
//wholeLayout->setStretchFactor(thirdHLayout, 1); setLayout(wholeLayout); connect(btnConstantPlus,SIGNAL(clicked()),this,SLOT(addConstant()));
connect(btnConstantMinus,SIGNAL(clicked()),this,SLOT(reduceConstant()));
} constantDiag::~constantDiag()
{ } void constantDiag::addConstant()
{
int row = model->rowCount();
model->insertRow(row);
} void constantDiag::reduceConstant()
{
int row = model->rowCount();
model->removeRow(--row);
} #include <QComboBox> ComboDelegate::ComboDelegate(QObject *parent) :
QItemDelegate(parent)
{
} QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const
{
QComboBox *editor = new QComboBox(parent);
//editor->lineEdit()->setAlignment(Qt::AlignCenter);
editor->setEditable(true);
//editor->setStyleSheet("QComboBox{border:1px solid gray;}""QComboBox QAbstractItemView::item{height:25px;}"); //editor->setView(new QListView());
editor->addItem("N");
editor->addItem("m");
editor->addItem("m/s");
editor->installEventFilter(const_cast<ComboDelegate*>(this));
return editor;
} void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
QString str =index.model()->data(index).toString();
QComboBox *box = static_cast<QComboBox*>(editor);
int i=box->findText(str);
box->setCurrentIndex(i);
} void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *box = static_cast<QComboBox*>(editor);
QString str = box->currentText();
model->setData(index,str);
} void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}

系统常量对话框QT实现的更多相关文章

  1. ThinPHP命名空间,连接数据库是要修改的配置文件,Model数据模型层,跨控制器调用,如何获取系统常量信息,

    一.命名空间(主要是为了实现自动加载类) *命名空间(相当于虚拟的目录),为了让类有一个统一的文件夹来管理(可以自动加载'类'),每个文件都要有命名空间*tp如何做命名空间:*TP框架下有一个初始命名 ...

  2. thinkphp系统常量与自定义常量

    ----------------------------------------Action中使用的系统常量 ----------------------------------------THINK ...

  3. PHPCMS系统常量

    以下系统常量全局可用 APP_PATH  动态程序路径 WEB_PATH  网站根路径 JS_PATH  JS路径 CSS_PATH CSS路径 IMG_PATH  图片路径 CACHE_PATH 缓 ...

  4. phpcms标签使用 —— 系统常量

    以下系统常量全局可用 1 2 3 4 5 6 7 8 9 10 11 12 13 CACHE_PATH 缓存文件夹地址 SITE_PROTOCOL 主机协议 SITE_URL 当前访问的主机名 HTT ...

  5. Paip.最佳实践-- Buildin variale 内建变量 ,魔术变量,预定义变量,系统常量,系统变量 1

    Paip.最佳实践-- Buildin variale 内建变量 ,魔术变量,预定义变量,系统常量,系统变量 1.1.1       C++内建变量(__LINE__).... 1.1.2       ...

  6. (转)Thinkphp系统常量 演示

    Thinkphp2.1框架内置了许多系统常量, 具体如下: __ROOT__ : 网站根目录地址 __APP__ : 当前项目(入口文件)地址 __URL__ : 当前模块地址 __ACTION__  ...

  7. [php基础]PHP环境变量$_SERVER和系统常量详细说明

    在PHP网站开发中,为了满足网站的需要,时常需要对PHP环境变量进行设置和应用,在虚拟主机环境下,有时我们更需要通过PHP环境变量操作函数来对PHP环境变量值进行设置.为此我们有必要对PHP环境变量先 ...

  8. php的系统常量

    认识一下系统常量 系统常量是PHP已经定义好的常量,我们可以直接拿来使用,常见的系统常量有: (1)__FILE__ :php程序文件名.它可以帮助我们获取当前文件在服务器的物理位置. (2)__LI ...

  9. tp系统常量定义

    (2013-03-06 14:16:31) 转载▼ 标签: it 是已经封装好的系统常量 主要是用在控制器下面的动作当中 这样能很大的提高我们的开发效率 主要有下面的一些      手册上面都有的   ...

随机推荐

  1. TinyMCE插件:Filemanager [4.x-6.x] 图片自动添加水印

    上传图片程序(filemanager/upload.php) 在if (!empty($_FILES) && $upload_files)有一个move_uploaded_file() ...

  2. React基本语法

    React 一.导入     0.局部安装 react 和 react-dom         npm install --save-dev react react-dom       1.react ...

  3. yii学习笔记(5),视图操作

    在控制器调用$this->render()方法来输出视图 function actionLogin(){ $name = "admin"; // 加载视图 return $t ...

  4. 我的名字叫hadoop

      第一回 新入环境 我的名字是hadoop,我一出生我的爸爸雅虎就给我取了这样一个名字:hadoop,我也不知道为什么叫这个名字,刚出生没多久,雅虎爸爸就把我领进一个黑暗的屋子里面,屋里堆满了黑色的 ...

  5. CentOS6升级Python2.6到3.7,错误处理[No module named '_ctypes']

    CentOS6升级Python2.6到3.7,错误处理[No module named '_ctypes'] 因开发需要,在CentOS 6 服务器将Python2进行升级到Python3.由于工作中 ...

  6. mfc和qt的区别

    注:引用来源 http://wenda.chinabaike.com/b/30934/2013/1208/707410.html QT使用的编译器是MinGW,即Linux下的GCC移植到window ...

  7. 安装Flutter环境

    mac 环境安装 系统需求 操作系统: macOS (64-bit) 硬盘: 700 MB 工具: bash, mkdir, rm, git, curl, unzip, which 环境安装 SDK ...

  8. 数据结构之栈和队列及其Java实现

    栈和队列是数据结构中非常常见和基础的线性表,在某些场合栈和队列使用很多,因此本篇主要介绍栈和队列,并用Java实现基本的栈和队列,同时用栈和队列相互实现. 栈:栈是一种基于“后进先出”策略的线性表.在 ...

  9. HBase简介(梳理知识)

    一. 简介 hbase是bigtable的开源山寨版本.是建立的hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.它介于nosql和RDBMS之间,仅能通过主键(row key ...

  10. 免考final linux提权与渗透入门——Exploit-Exercise Nebula学习与实践

    免考final linux提权与渗透入门--Exploit-Exercise Nebula学习与实践 0x0 前言 Exploit-Exercise是一系列学习linux下渗透的虚拟环境,官网是htt ...