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. 验证XML文档的范例代码

    如果想变成自己的,就把这里的xml文档名替换成自己xml文档名 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc. ...

  2. 虚拟机下linux 系统网卡配置、固定IP地址

    1.进入该目录下修改内容 vi       /etc/sysconfig/network-scripts/  ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static DEF ...

  3. PAT A1127 ZigZagging on a Tree (30 分)

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  4. python新手第一天学习笔记-第一个ptyhon程序和python变量

    一.python 的注释和第一个python 程序 : 1.单行注释 # Author Xiajq 2.多行注释 ''' ------------注释内容----------------------- ...

  5. mybatis中的resultMap实际作用

    resultMap和resultType在实际的使用上完全可以进行替换,但是resultMap有比resultType更多的一个功能.我们先定义一个简单的resultMap例子 <resultM ...

  6. python是一门解释性语言吗?

    其实这只能算说对了一半,准确来说是编译跟解释性语言.python跟java.C# 一样都是会预编译一部分代码(简称做了优化) 都知道java编译要先在cmd里敲 javac hello.world 是 ...

  7. JDK核心源码(2)

    Java的基础知识有很多,但是我认为最基础的知识应该要属jdk的基础代码, jdk的基础代码里面,有分了很多基础模块,其中又属jdk包下面的lang包最为基础. 我们下面将总结和分析一下lang包下面 ...

  8. Mac OS下Android Studio:/dev/kvm not found

    在配置模拟器时出现该报错,在网上找了很多教程都没能解决,当然可能是这些教程并不适用于我.总的来说,还是要“对症下药”! 解决方法如下: 点击“系统偏好设置”-“安全性与隐私”,然后会在“通用”这个界面 ...

  9. HI-2110的657sp3版本应用笔记之TUP

    1. TUP是什么? TUP是华为的搞的一套封装了标准Coap的函数,底层是Coap,上层是华为封装的一层收发函数,用来简化Coap的收发流程,最终只用6个函数搞定,不用懂Coap就可以的. 2. T ...

  10. 抽样分布(2) t分布

    定义 t分布 设X ~ N(0,1),Y ~ χ2(n),且X,Y相互独立,则称随机变量 服从自由度为n的t分布(学生氏分布) 记为 t~t(n),其概率密度为 由于tn(x)是偶函数,其图形关于y轴 ...