QT Demo
2016-05-21 00:28
501 查看
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<QObject> #include<QString> #include<QButtonGroup> #include<QCheckBox> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); void paintEvent(QPaintEvent *); ~MainWindow(); public slots: void chkChanged(); void nonChkChanged(); private: Ui::MainWindow *ui; QCheckBox *exclusive[3]; QCheckBox *non_exclusive[3]; QButtonGroup *chk_group[2]; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QString str1[3] = {"Game","Office","Develop"}; QString str2[3] = {"P&Programing","Q&t","O&S"}; int xpos = 30; int ypos = 30; chk_group[0] = new QButtonGroup(this); chk_group[1] = new QButtonGroup(this); for(int i=0;i<3;i++){ exclusive[i] = new QCheckBox(str1[i],this); exclusive[i]->setGeometry(xpos,ypos,100,30); chk_group[0]->addButton(exclusive[i]); non_exclusive[i] = new QCheckBox(str2[i],this); non_exclusive[i] ->setGeometry(xpos+120,ypos,100,30); chk_group[1]->addButton(non_exclusive[i]); connect(exclusive[i],SIGNAL(clicked()),this,SLOT(chkChanged())); connect(non_exclusive[i],SIGNAL(clicked()),this,SLOT(nonChkChanged())); ypos+=40; } chk_group[0]->setExclusive(false); chk_group[1]->setExclusive(true); } void MainWindow::paintEvent(QPaintEvent *){ } void MainWindow::nonChkChanged(){ for(int i=0;i<3;i++){ if(non_exclusive[i]->isChecked()){ qDebug("non_checkbox %d selected ",i+1); } } } void MainWindow::chkChanged(){ for(int i=0;i<3;i++){ if(exclusive[i]->isChecked()){ qDebug("checkbox %d selected",i+1); } if(exclusive[i]->checkState()){ qDebug("checkbox %d selected",i+1); } } } MainWindow::~MainWindow() { delete ui; // delete []exclusive;//不要delete否则程序出错 // delete []non_exclusive; // delete []chk_group; }
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"/> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>23</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
#------------------------------------------------- # # Project created by QtCreator 2016-05-20T23:29:05 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = TestControllerUI TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui
相关文章推荐
- Qt学习之始
- QT中QPF字库的制作与旋转字体的实现
- QT中文字体的显示方法
- QT接收SendMessage/PostMessage发出的消息
- Qt根据汉字生成位图,可连续调用,生成的位图不会有杂点
- centos系统下,Qt中使用openGL和cuda,.pro需要配置的库和头文件
- QT类之事件mousePressEvent以及mouseMoveEvent
- openCV2.4.9与QT5.5.1结合打开摄像头并显示采集到的图像的问题
- 移植QT5.4.0到ARM开发板
- qt的容器类
- VS2008集成QT4.7.2环境搭建
- Qt 使用Windows API 函数精确控制循环执行的周期时间
- Qt编译遇到/usr/bin/ld: cannot find -lGL问题的解决方案
- 项目与软件推荐之编辑器-QOwnNotes
- Qt 获取当前时间
- 如何在QT中显示中文字符
- 指针操作:【申请】、【释放】、【YUV2BGR】、【QT保存图像】
- QT4的源代码在QT5的环境下编译
- Qt程序提示: 启动程序失败,路径或者权限错误解决方案
- QT-4.7.2的移植过程