标签:bsp gray data min index ade geometry qstring exec
1.运行结果:
2.代码
main.cpp
1 #include "constantdiag.h" 2 #include <QtWidgets/QApplication> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 constantDiag w; 8 w.show(); 9 return a.exec(); 10 }
constantDialog.h
1 #ifndef CONSTANTDIAG_H 2 #define CONSTANTDIAG_H 3 4 #include <QtWidgets/QWidget> 5 #include <QHBoxLayout> 6 #include <QPushButton> 7 #include <QStandardItemModel> 8 #include <QTableView> 9 #include <QVBoxLayout> 10 #include <QHeaderView> 11 #include <QLabel> 12 13 class ComboDelegate; 14 15 class constantDiag : public QWidget 16 { 17 Q_OBJECT 18 19 public: 20 constantDiag(QWidget *parent = 0); 21 ~constantDiag(); 22 23 public slots: 24 void addConstant(); 25 void reduceConstant(); 26 27 28 private: 29 QHBoxLayout *secondHLayout; 30 QPushButton *btnConstantPlus; 31 QPushButton *btnConstantMinus; 32 QStandardItemModel *model; 33 QTableView *tableview; 34 QVBoxLayout *wholeLayout; 35 QHBoxLayout *thirdHLayout; 36 QPushButton *btnOk; 37 QPushButton *btnCancel; 38 QHBoxLayout *topLayout; 39 QLabel *titleLabel; 40 QVBoxLayout *wholeVLayout; 41 ComboDelegate *comboDelegate; 42 }; 43 44 #endif // CONSTANTDIAG_H 45 46 47 #ifndef COMBODELEGATE_H 48 #define COMBODELEGATE_H 49 50 #include <QItemDelegate> 51 52 class ComboDelegate : public QItemDelegate 53 { 54 Q_OBJECT 55 public: 56 ComboDelegate(QObject *parent = 0); 57 58 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; 59 void setEditorData(QWidget *editor, const QModelIndex &index) const; 60 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; 61 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; 62 }; 63 64 #endif // COMBODELEGATE_H
constantDialog.cpp
1 #include "constantdiag.h" 2 3 constantDiag::constantDiag(QWidget *parent) 4 : QWidget(parent) 5 { 6 setWindowTitle(QString::fromLocal8Bit("系统常量")); 7 resize(480,168); 8 9 wholeLayout = new QVBoxLayout(parent); 10 11 secondHLayout = new QHBoxLayout(); 12 secondHLayout->addStretch(); 13 14 btnConstantPlus = new QPushButton; 15 btnConstantPlus->setText("+"); 16 secondHLayout->addWidget(btnConstantPlus); 17 18 btnConstantMinus = new QPushButton; 19 btnConstantMinus->setText("-"); 20 secondHLayout->addWidget(btnConstantMinus); 21 22 secondHLayout->setStretch(0,5); 23 secondHLayout->setStretch(1,1); 24 secondHLayout->setStretch(2,1); 25 26 wholeLayout->addLayout(secondHLayout); 27 28 model = new QStandardItemModel(2,3); 29 model->setHeaderData(0,Qt::Horizontal,QString::fromLocal8Bit("名称")); 30 model->setHeaderData(1,Qt::Horizontal,QString::fromLocal8Bit("值")); 31 model->setHeaderData(2,Qt::Horizontal,QString::fromLocal8Bit("单位")); 32 tableview = new QTableView(parent); 33 tableview->setModel(model); 34 tableview->verticalHeader()->setVisible(false); 35 tableview->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); 36 tableview->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); 37 tableview->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch); 38 comboDelegate = new ComboDelegate; 39 tableview->setItemDelegateForColumn(2,comboDelegate); 40 //tableview->verticalHeader()->setSectionResizeMode(0,QHeaderView::Stretch); 41 //tableview->verticalHeader()->setSectionResizeMode(1,QHeaderView::Stretch); 42 43 wholeLayout->addWidget(tableview); 44 45 thirdHLayout = new QHBoxLayout; 46 thirdHLayout->addStretch(); 47 48 btnOk = new QPushButton; 49 btnOk->setText(QString::fromLocal8Bit("确定")); 50 btnCancel = new QPushButton; 51 btnCancel->setText(QString::fromLocal8Bit("取消")); 52 thirdHLayout->addWidget(btnOk); 53 thirdHLayout->addWidget(btnCancel); 54 55 thirdHLayout->setStretch(0,5); 56 thirdHLayout->setStretch(1,1); 57 thirdHLayout->setStretch(2,1); 58 59 wholeLayout->addLayout(thirdHLayout); 60 //wholeLayout->setSizeConstraint(QLayout::SetFixedSize); 61 //wholeLayout->setStretchFactor(secondHLayout, 1); 62 //wholeLayout->setStretchFactor(tableview, 2); 63 //wholeLayout->setStretchFactor(thirdHLayout, 1); 64 65 setLayout(wholeLayout); 66 67 connect(btnConstantPlus,SIGNAL(clicked()),this,SLOT(addConstant())); 68 connect(btnConstantMinus,SIGNAL(clicked()),this,SLOT(reduceConstant())); 69 } 70 71 constantDiag::~constantDiag() 72 { 73 74 } 75 76 void constantDiag::addConstant() 77 { 78 int row = model->rowCount(); 79 model->insertRow(row); 80 } 81 82 void constantDiag::reduceConstant() 83 { 84 int row = model->rowCount(); 85 model->removeRow(--row); 86 } 87 88 89 #include <QComboBox> 90 91 ComboDelegate::ComboDelegate(QObject *parent) : 92 QItemDelegate(parent) 93 { 94 } 95 96 QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const 97 { 98 QComboBox *editor = new QComboBox(parent); 99 //editor->lineEdit()->setAlignment(Qt::AlignCenter); 100 editor->setEditable(true); 101 //editor->setStyleSheet("QComboBox{border:1px solid gray;}""QComboBox QAbstractItemView::item{height:25px;}"); 102 103 //editor->setView(new QListView()); 104 editor->addItem("N"); 105 editor->addItem("m"); 106 editor->addItem("m/s"); 107 editor->installEventFilter(const_cast<ComboDelegate*>(this)); 108 return editor; 109 } 110 111 void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const 112 { 113 QString str =index.model()->data(index).toString(); 114 QComboBox *box = static_cast<QComboBox*>(editor); 115 int i=box->findText(str); 116 box->setCurrentIndex(i); 117 } 118 119 void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 120 { 121 QComboBox *box = static_cast<QComboBox*>(editor); 122 QString str = box->currentText(); 123 model->setData(index,str); 124 } 125 126 void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const 127 { 128 editor->setGeometry(option.rect); 129 }
标签:bsp gray data min index ade geometry qstring exec
原文地址:http://www.cnblogs.com/wang-kai/p/6529978.html