1 //2.5 控件 2 //2.51 按钮组(Buttons) 3 /* 4 Radio Button //单选按钮 5 Check Box//复选框 6 Dialog Button Box//对话框按钮盒 7 */ 8 //main.cpp 9 #include "mywidget.h" 10 #include <QApplication> 11 12 int main(int argc, char *argv[]) 13 { 14 QApplication a(argc, argv); 15 MyWidget w;//创建一个MyWidget对象 16 w.setGeometry(100,100,200,120);//设置其长宽 17 w.show(); 18 19 return a.exec(); 20 } 21 //mywidget.h 22 #include "mywidget.h" 23 #include <qapplication.h> 24 #include <qpushbutton.h>//用到了按钮,因此添加相应的头文件 25 #include <qfont.h> 26 MyWidget::MyWidget(QWidget *parent) 27 : QWidget(parent) 28 { 29 setMinimumSize(200,120); 30 setMaximumSize(200,120); 31 QPushButton *quit = new QPushButton("Quit",this);//先记住 32 quit->setGeometry(62,40,75,30); 33 quit->setFont(QFont("Times",18,QFont::Bold)); 34 connect(quit,SIGNAL(clicked()),qApp,SLOT(quit())); 35 } 36 37 MyWidget::~MyWidget() 38 { 39 40 } 41 //2.52 输入部件组(Input Widgets) 42 /* 43 combo box 组合框 44 spin box 数字显示框 45 dial 拨号 46 scroll bar 滚动条 47 slider 滑块 48 key sequence edit 按键序列编辑框 49 */ 50 //QDate Time 51 QLabel * datalabel = new QLabel(); 52 QDateTime * datatime = new QDateTime(QDateTime::currentDateTime()); 53 datalabel->setText(datatime->date().toString()); 54 datalabel->show(); 55 //QTime 56 QTimer *time_clock = new QTimer(parent); 57 connect(time_clock,SIGNAL(timeout()),this,SLOT(slottimedone()));//定时时间一到发送timeout()信号,从而触发slottimedone()槽去完成某件事情 58 start(int time)//每隔time秒触发定时 59 setSingleShot(true)//仅启动一次 60 stop()//关掉定时器 61 time_clock->start(2000)//示例 62 //2.53 显示控件组(Display Widgets) 63 LCD Number//液晶数字 64 Progress Bar//进度条 65 OpenGL widget//开放式图形库工具 66 QQuickWidget//嵌入QML工具 67 68 Text Browser 介绍 69 属性见 P48 70 SLOTS: 71 virtual void backward() 72 virtual void forward() 73 virtual void home() 74 75 //widget.h 76 #ifndef WIDGET_H 77 #define WIDGET_H 78 79 #include <QWidget> 80 #include <QTreeWidgetItem> 81 namespace Ui { 82 class Widget; 83 } 84 85 class Widget : public QWidget 86 { 87 Q_OBJECT 88 89 public: 90 explicit Widget(QWidget *parent = 0); 91 void init(); 92 ~Widget(); 93 void updateParentItem(QTreeWidgetItem* item); 94 public slots: 95 void treeItemChanged (QTreeWidgetItem* item,int column); 96 97 private: 98 Ui::Widget *ui; 99 }; 100 101 #endif // WIDGET_H 102 103 //widget.cpp 104 #include "widget.h" 105 #include "ui_widget.h" 106 107 Widget::Widget(QWidget *parent) : 108 QWidget(parent), 109 ui(new Ui::Widget) 110 { 111 init();//? 112 connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));//SIGNAL & SLot 113 ui->setupUi(this); 114 } 115 116 Widget::~Widget() 117 { 118 delete ui; 119 } 120 void Widget::init() 121 { 122 ui->treeWidget->clear(); 123 //1 124 QTreeWidgetItem *group1 = new QTreeWidgetItem(ui->treeWidget); 125 group1->setText(0,"group1"); 126 group1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 127 group1->setCheckState(0,Qt::Unchecked); 128 QTreeWidgetItem *subItem11 = new QTreeWidgetItem(group1); 129 subItem11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 130 subItem11->setText(0,"subItem11"); 131 subItem11->setCheckState(0,Qt::Unchecked); 132 QTreeWidgetItem *subItem12 = new QTreeWidgetItem(group1); 133 subItem12->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 134 subItem12->setText(0,"subItem12"); 135 subItem12->setCheckState(0,Qt::Unchecked); 136 QTreeWidgetItem *subItem13 = new QTreeWidgetItem(group1); 137 subItem13->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 138 subItem13->setText(0,"subItem13"); 139 subItem13->setCheckState(0,Qt::Unchecked); 140 //2 141 QTreeWidgetItem *group2 = new QTreeWidgetItem(ui->treeWidget); 142 group2->setText(0,"group2"); 143 group2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 144 group2->setCheckState(0,Qt::Unchecked); 145 QTreeWidgetItem *subItem21 = new QTreeWidgetItem(group2); 146 subItem21->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 147 subItem21->setText(0,"subItem21"); 148 subItem21->setCheckState(0,Qt::Unchecked); 149 QTreeWidgetItem *subItem22 = new QTreeWidgetItem(group2); 150 subItem22->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 151 subItem22->setText(0,"subItem22"); 152 subItem22->setCheckState(0,Qt::Unchecked); 153 QTreeWidgetItem *subItem23 = new QTreeWidgetItem(group2); 154 subItem23->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 155 subItem23->setText(0,"subItem23"); 156 subItem23->setCheckState(0,Qt::Unchecked); 157 } 158 159 void Widget::treeItemChanged(QTreeWidgetItem *item, int column) 160 { 161 QString itemText = item->text(0); 162 if (Qt::Checked==item->checkState(0)) 163 { 164 QTreeWidgetItem* parent = item->parent(); 165 int count=item->childCount(); 166 if(count>0) 167 { 168 for(int i=0;i<count;i++) 169 { 170 item->child(i)->setCheckState(0,Qt::Checked); 171 } 172 } 173 else 174 { 175 updateParentItem(item); 176 } 177 } 178 else if(Qt::Unchecked == item->checkState(0)) 179 { 180 int count = item->childCount(); 181 if(count>0) 182 { 183 for(int i=0;i<count;i++) 184 { 185 item->child(i)->setCheckState(0,Qt::Unchecked); 186 } 187 } 188 else 189 { 190 updateParentItem(item); 191 } 192 } 193 } 194 void Widget::updateParentItem(QTreeWidgetItem *item) 195 { 196 QTreeWidgetItem *parent = item->parent(); 197 if(parent == NULL) 198 { 199 return; 200 } 201 int selectedCount = 0; 202 int childCount = parent->childCount(); 203 for(int i =0;i<childCount;++i) 204 { 205 QTreeWidgetItem* childItem = parent->child(i); 206 if(childItem->checkState(0)==Qt::Checked) 207 { 208 selectedCount++; 209 } 210 } 211 if(selectedCount<=0) 212 { 213 parent->setCheckState(0,Qt::Unchecked); 214 } 215 else if(selectedCount>0&&selectedCount<childCount) 216 { 217 parent->setCheckState(0,Qt::PartiallyChecked); 218 } 219 else if(selectedCount == childCount) 220 { 221 parent->setCheckState(0,Qt::Checked); 222 } 223 } 224 225 226 //main.cpp 227 #include "widget.h" 228 #include <QApplication> 229 230 int main(int argc, char *argv[]) 231 { 232 QApplication a(argc, argv); 233 Widget w; 234 w.show(); 235 236 return a.exec(); 237 } 238 239 240 //dialog.h 241 private slots: 242 void on_lineEdit_textChanged();//定义一个槽;名字的定义有一定规则 243 //dialog.cpp 244 Dialog::Dialog(QWidget *parent) : 245 QDialog(parent), 246 ui(new Ui::Dialog) 247 { 248 ui->setupUi(this); 249 QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");//建立一个正则表达式 250 ui->lineEdit->setValidator(new QRegExpValidator(regExp,this));//先记住如何使用 251 connect(ui->okButton,SIGNAL(clicked(bool)),this,SLOT(accept()));//信号与槽的连接 252 connect(ui->cancelButton,SIGNAL(clicked(bool)),this,SLOT(reject())); 253 } 254 void Dialog::on_lineEdit_textChanged()//定义槽函数 255 { 256 ui->okButton->setEnabled(ui->lineEdit->hasAcceptableInput());//使用lineEdit输入文本的有效性决定okButton的可操作性 257 }
原文地址:https://www.cnblogs.com/justsong/p/8361081.html
时间: 2024-12-24 14:22:37