组件
QPushButton
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QPushButton>
# include <QDebug>
class Widget : public QWidget
{
Q_OBJECT
public :
Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void buttonClick ( ) ;
private :
QPushButton * btn;
} ;
# endif
# include "widget.h"
Widget :: Widget ( QWidget * parent)
: QWidget ( parent)
{
this -> setFixedSize ( 960 , 640 ) ;
this -> setWindowTitle ( "应用程序" ) ;
btn = new QPushButton ( "按钮" , this ) ;
btn-> setParent ( this ) ;
btn-> setText ( "点击" ) ;
btn-> setToolTip ( "按钮" ) ;
btn-> setFixedSize ( 100 , 50 ) ;
btn-> move ( ( this -> width ( ) - btn-> width ( ) ) / 2 , ( this -> height ( ) - btn-> height ( ) ) / 2 ) ;
btn-> setGeometry ( 480 - 50 , 320 - 25 , 100 , 50 ) ;
this -> setWindowIcon ( QIcon ( ":/img/1.png" ) ) ;
btn-> setStyleSheet ( "border-image:url(:/img/1.png);" ) ;
btn-> x ( ) ;
btn-> y ( ) ;
btn-> pos ( ) ;
QPushButton * btn1 = new QPushButton ( QIcon ( ":/img/1.png" ) , "click" , this ) ;
connect ( btn, SIGNAL ( clicked ( bool ) ) , this , SLOT ( buttonClick ( ) ) ) ;
connect ( btn1, SIGNAL ( clicked ( bool ) ) , this , SLOT ( buttonClick ( ) ) ) ;
}
void Widget :: buttonClick ( )
{
qDebug ( ) << "hello world" ;
}
Widget :: ~ Widget ( )
{
}
QLabel
# ifndef WIDGET_H
# define WIDGET_H
# include <QPushButton>
# include <QLabel>
# include <QMovie>
# include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public :
Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void btn_click ( ) ;
private :
QLabel * lab;
QPushButton * btn;
} ;
# endif
# include "widget.h"
Widget :: Widget ( QWidget * parent)
: QWidget ( parent)
{
this -> setFixedSize ( 960 , 640 ) ;
lab = new QLabel;
btn = new QPushButton;
this -> lab-> setParent ( this ) ;
this -> btn-> setParent ( this ) ;
this -> lab-> setFixedSize ( 950 , 500 ) ;
this -> btn-> setFixedSize ( 100 , 50 ) ;
this -> btn-> setText ( "点击" ) ;
this -> lab-> setStyleSheet ( "background-color:green;" ) ;
this -> lab-> setAlignment ( Qt:: AlignCenter| Qt:: AlignHCenter) ;
this -> lab-> move ( 5 , 5 ) ;
this -> btn-> move ( ( this -> width ( ) - this -> btn-> width ( ) ) / 2 , this -> lab-> height ( ) + 50 ) ;
connect ( btn, & QPushButton:: clicked, this , & Widget:: btn_click) ;
}
void Widget :: btn_click ( )
{
this -> lab-> setText ( "hello world" ) ;
this -> lab-> setPixmap ( QPixmap ( "1.png" ) . scaled ( lab-> width ( ) , lab-> height ( ) ) ) ;
this -> lab-> setPixmap ( QPixmap ( "1.png" ) ) ;
QMovie * move = new QMovie ( ) ;
move-> setFileName ( "3.gif" ) ;
lab-> setMovie ( move) ;
move-> start ( ) ;
}
Widget :: ~ Widget ( )
{
}
QLineEdit
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private :
Ui:: Widget * ui;
private slots:
void textChanged ( QString text) ;
void returnPressed ( ) ;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
# include "widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
this -> setFixedSize ( 640 , 480 ) ;
ui-> lineEdit-> setFixedSize ( 200 , 40 ) ;
ui-> lineEdit_2-> setFixedSize ( ui-> lineEdit-> size ( ) ) ;
ui-> lineEdit_2-> move ( ui-> lineEdit-> x ( ) , ui-> lineEdit-> y ( ) + ui-> lineEdit-> height ( ) + 10 ) ;
ui-> lineEdit_3-> setFixedSize ( ui-> lineEdit-> size ( ) ) ;
ui-> lineEdit_3-> move ( ui-> lineEdit-> x ( ) , ui-> lineEdit_2-> y ( ) + ui-> lineEdit_2-> height ( ) + 10 ) ;
ui-> lineEdit-> setPlaceholderText ( "用户名" ) ;
ui-> lineEdit_2-> setPlaceholderText ( "密码" ) ;
ui-> lineEdit_3-> setPlaceholderText ( "验证码" ) ;
ui-> lineEdit-> setAlignment ( Qt:: AlignRight) ;
ui-> lineEdit_2-> setAlignment ( Qt:: AlignRight) ;
ui-> lineEdit_3-> setAlignment ( Qt:: AlignRight) ;
ui-> lineEdit_2-> setEchoMode ( QLineEdit:: Password) ;
ui-> lineEdit_3-> setEchoMode ( QLineEdit:: PasswordEchoOnEdit) ;
connect ( ui-> lineEdit, & QLineEdit:: textChanged, this , & Widget:: textChanged) ;
connect ( ui-> lineEdit_2, & QLineEdit:: returnPressed, this , & Widget:: returnPressed) ;
connect ( ui-> lineEdit_3, & QLineEdit:: selectionChanged, this , [ & ] ( ) {
ui-> label-> setText ( ui-> lineEdit_3-> selectedText ( ) ) ;
} ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: textChanged ( QString text)
{
ui-> label-> setText ( text) ;
}
void Widget :: returnPressed ( )
{
ui-> label-> setText ( ui-> lineEdit_2-> text ( ) ) ;
}
QCheckBox
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private :
Ui:: Widget * ui;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
# include <QDebug>
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
connect ( ui-> checkBox, & QCheckBox:: stateChanged, this , [ & ] ( int state) {
qDebug ( ) << state;
} ) ;
connect ( ui-> pushButton, & QPushButton:: clicked, this , [ & ] ( ) {
if ( ui-> checkBox-> isChecked ( ) )
{
ui-> label-> setText ( ui-> lineEdit-> text ( ) ) ;
}
else
{
ui-> checkBox-> setCheckState ( Qt:: Checked) ;
}
} ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
QRadioButton
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QGroupBox>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_clicked ( ) ;
private :
Ui:: Widget * ui;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_pushButton_clicked ( )
{
if ( ui-> radioButton-> isChecked ( ) )
{
ui-> label-> setText ( "A" ) ;
}
if ( ui-> radioButton_2-> isChecked ( ) )
{
ui-> label-> setText ( "B" ) ;
}
if ( ui-> radioButton_3-> isChecked ( ) )
{
ui-> label-> setText ( "C" ) ;
}
if ( ui-> radioButton_4-> isChecked ( ) )
{
ui-> label-> setText ( "D" ) ;
}
QGroupBox group;
}
其他
QLayout
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QPushButton>
# include <QTextEdit>
# include <QLayout>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private :
Ui:: Widget * ui;
private :
QPushButton * btn_1;
QPushButton * btn_2;
QPushButton * btn_3;
QPushButton * btn_4;
QPushButton * btn_5;
QTextEdit * text;
QHBoxLayout * hlayout;
QVBoxLayout * vlayout;
QGridLayout * glayout;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
btn_1 = new QPushButton ( "1" , this ) ;
btn_2 = new QPushButton ( "2" , this ) ;
btn_3 = new QPushButton ( "3" , this ) ;
btn_4 = new QPushButton ( "4" , this ) ;
btn_5 = new QPushButton ( "5" , this ) ;
btn_1-> setMaximumSize ( QSize ( 260 , 80 ) ) ;
btn_2-> setMaximumSize ( QSize ( 260 , 80 ) ) ;
btn_3-> setMaximumSize ( QSize ( 260 , 80 ) ) ;
btn_4-> setMaximumSize ( QSize ( 260 , 80 ) ) ;
btn_5-> setMaximumSize ( QSize ( 260 , 80 ) ) ;
btn_1-> setSizePolicy ( QSizePolicy:: Expanding, QSizePolicy:: Expanding) ;
btn_2-> setSizePolicy ( QSizePolicy:: Expanding, QSizePolicy:: Expanding) ;
btn_3-> setSizePolicy ( QSizePolicy:: Expanding, QSizePolicy:: Expanding) ;
btn_4-> setSizePolicy ( QSizePolicy:: Expanding, QSizePolicy:: Expanding) ;
btn_5-> setSizePolicy ( QSizePolicy:: Expanding, QSizePolicy:: Expanding) ;
text = new QTextEdit;
# if 1
vlayout = new QVBoxLayout ( this ) ;
hlayout = new QHBoxLayout;
hlayout-> setSpacing ( 6 ) ;
hlayout-> addWidget ( btn_1) ;
hlayout-> addWidget ( btn_2) ;
hlayout-> addWidget ( btn_3) ;
hlayout-> addWidget ( btn_4) ;
hlayout-> addWidget ( btn_5) ;
vlayout-> setSpacing ( 6 ) ;
vlayout-> setContentsMargins ( 11 , 11 , 11 , 11 ) ;
vlayout-> addLayout ( hlayout) ;
vlayout-> addWidget ( text) ;
# else
glayout = new QGridLayout ( this ) ;
glayout-> addWidget ( btn_1, 0 , 0 , 1 , 1 ) ;
glayout-> addWidget ( btn_2, 0 , 1 , 1 , 1 ) ;
glayout-> addWidget ( btn_3, 0 , 2 , 1 , 1 ) ;
glayout-> addWidget ( btn_4, 0 , 3 , 1 , 1 ) ;
glayout-> addWidget ( btn_5, 0 , 4 , 1 , 1 ) ;
glayout-> addWidget ( text, 1 , 0 , 6 , 5 ) ;
# endif
}
Widget :: ~ Widget ( )
{
delete ui;
}
QMainWindow
# ifndef MAINWINDOW_H
# define MAINWINDOW_H
# include <QMainWindow>
# include <QMenubar>
# include <QMenu>
# include <QAction>
# include <QToolBar>
# include <QStatusBar>
# include <QDockWidget>
# include <QPushButton>
# include <QLabel>
# include <QLineEdit>
# include <QTextEdit>
class MainWindow : public QMainWindow
{
Q_OBJECT
public :
MainWindow ( QWidget * parent = 0 ) ;
~ MainWindow ( ) ;
private :
QMenu * fileMenu;
QAction * openAction, * newAction, * saveAction, * closeAction;
QPushButton * btn;
QLineEdit * line;
QLabel * lab;
private :
void btnClick ( ) ;
} ;
# endif
# include "mainwindow.h"
MainWindow :: MainWindow ( QWidget * parent)
: QMainWindow ( parent)
{
this -> setMinimumSize ( 640 , 480 ) ;
fileMenu = menuBar ( ) -> addMenu ( "文件" ) ;
QMenu * editMenu = new QMenu ( "编辑" ) ;
menuBar ( ) -> addMenu ( editMenu) ;
menuBar ( ) -> addMenu ( "构建" ) ;
openAction = fileMenu-> addAction ( "打开" ) ;
newAction = fileMenu-> addAction ( "新建" ) ;
openAction-> setStatusTip ( "open" ) ;
newAction-> setShortcut ( QKeySequence:: New) ;
openAction-> setShortcut ( QKeySequence ( "ctrl+o" ) ) ;
saveAction = new QAction ( "保存" ) ;
saveAction-> setShortcut ( Qt:: CTRL+ Qt:: Key_S) ;
fileMenu-> addAction ( saveAction) ;
closeAction = fileMenu-> addAction ( "关闭" ) ;
QMenu * childMenu = fileMenu-> addMenu ( "最近打开的文件或项目" ) ;
childMenu-> addAction ( "1" ) ;
childMenu-> addAction ( "2" ) ;
childMenu-> addAction ( "3" ) ;
childMenu-> addAction ( "4" ) ;
childMenu-> addAction ( "5" ) ;
QToolBar * tool1 = addToolBar ( "file" ) ;
QAction * first = tool1-> addAction ( "1" ) ;
first-> setStatusTip ( "open" ) ;
tool1-> addAction ( "1" ) ;
tool1-> addAction ( "1" ) ;
tool1-> addAction ( "1" ) ;
tool1-> setAllowedAreas ( Qt:: LeftToolBarArea | Qt:: TopToolBarArea) ;
QToolBar * tool2 = addToolBar ( "edit" ) ;
btn = new QPushButton ( "点击" ) ;
line = new QLineEdit ( "无" ) ;
lab = new QLabel;
tool2-> addWidget ( lab) ;
tool2-> addWidget ( btn) ;
tool2-> addWidget ( line) ;
connect ( btn, & QPushButton:: clicked, this , & MainWindow:: btnClick) ;
QDockWidget * dock = new QDockWidget ( "工作区1" ) ;
dock-> setAllowedAreas ( Qt:: LeftDockWidgetArea| Qt:: RightDockWidgetArea) ;
QTextEdit * text = new QTextEdit;
dock-> setWidget ( text) ;
this -> addDockWidget ( Qt:: LeftDockWidgetArea, dock) ;
QDockWidget * dock2 = new QDockWidget ( "工作区2" ) ;
dock2-> setAllowedAreas ( Qt:: LeftDockWidgetArea| Qt:: RightDockWidgetArea) ;
QTextEdit * text2 = new QTextEdit;
dock2-> setWidget ( text2) ;
this -> addDockWidget ( Qt:: LeftDockWidgetArea, dock2) ;
statusBar ( ) -> showMessage ( tr ( "Ready" ) , 5000 ) ;
}
MainWindow :: ~ MainWindow ( )
{
}
void MainWindow :: btnClick ( )
{
QString text = line-> text ( ) ;
lab-> setText ( text) ;
}
QDialog
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QFileDialog>
# include <QInputDialog>
# include <QColorDialog>
# include <QMessageBox>
# include <QFontDialog>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_5_clicked ( ) ;
void on_pushButton_2_clicked ( ) ;
void on_pushButton_clicked ( ) ;
void on_pushButton_3_clicked ( ) ;
void on_pushButton_4_clicked ( ) ;
private :
Ui:: Widget * ui;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
# include <QDebug>
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_pushButton_5_clicked ( )
{
# if 0
QString filename = QFileDialog :: getOpenFileName ( ) ;
ui-> textEdit-> append ( filename) ;
# elif 0
QString filename = QFileDialog :: getOpenFileName ( this , "open" , "D:/" , " all file(*);;c file(*.c);;cpp file(*.cpp)" ) ;
ui-> textEdit-> append ( filename) ;
# elif 1
QStringList filenames = QFileDialog :: getOpenFileNames ( this , "open files" , "D:/" , " all file(*);;c(*.c);;cpp(*.cpp);;image(*jpg *.png)" ) ;
for ( int i= 0 ; i< filenames. size ( ) ; i++ )
ui-> textEdit-> append ( filenames. at ( i) ) ;
# endif
}
void Widget :: on_pushButton_2_clicked ( )
{
# if 0
QString msg = QInputDialog :: getText ( this , "输入" , "请输入信息" ) ;
ui-> textEdit-> append ( msg) ;
# elif 1
int num = QInputDialog :: getInt ( this , "input" , "please input int" ) ;
ui-> textEdit-> append ( QString ( "input %1" ) . arg ( num) ) ;
# endif
}
void Widget :: on_pushButton_clicked ( )
{
# if 0
QMessageBox :: about ( this , "version" , "5.11.1" ) ;
# elif 0
int log = QMessageBox :: question ( this , "????" , "5.11.1" , QMessageBox:: Yes, QMessageBox:: No) ;
qDebug ( ) << "log:" << log;
if ( log== QMessageBox:: Yes)
{
ui-> textEdit-> append ( "is 5.11.1" ) ;
}
else
{
ui-> textEdit-> append ( "no 5.11.1" ) ;
}
# elif 0
int log = QMessageBox :: warning ( this , "warning" , "5.11.1" , "in" , "out" , "mid" ) ;
qDebug ( ) << "log:" << log;
if ( log== 0 )
{
ui-> textEdit-> append ( "in 5.11.1" ) ;
}
else if ( log== 2 )
{
ui-> textEdit-> append ( "mid 5.11.1" ) ;
}
else
{
ui-> textEdit-> append ( "out 5.11.1" ) ;
}
# elif 1
QMessageBox :: critical ( this , "critical" , "5.11.1" , "i know" ) ;
# endif
}
void Widget :: on_pushButton_3_clicked ( )
{
QColor color = QColorDialog :: getColor ( Qt:: red, this , "color" ) ;
qDebug ( ) << color;
}
void Widget :: on_pushButton_4_clicked ( )
{
bool ok;
QFont font = QFontDialog :: getFont ( & ok, QFont ( "Times" , 12 ) , this ) ;
qDebug ( ) << font;
}
QEvent
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QKeyEvent>
# include <QTimerEvent>
# include <QEvent>
# include <QLabel>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
# if 1
void keyPressEvent ( QKeyEvent * event) ;
void mousePressEvent ( QMouseEvent * event) ;
void mouseReleaseEvent ( QMouseEvent * event) ;
void mouseMoveEvent ( QMouseEvent * event) ;
void timerEvent ( QTimerEvent * event) ;
bool event ( QEvent * event) ;
# endif
bool eventFilter ( QObject * watched, QEvent * event) ;
private :
Ui:: Widget * ui;
QLabel * lbl;
int timeId;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
lbl = new QLabel ( "OOOOOOOO" , this ) ;
this -> setMouseTracking ( true ) ;
ui-> label_2-> installEventFilter ( this ) ;
ui-> label_3-> installEventFilter ( this ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
# include <QDebug>
# if 1
void Widget :: keyPressEvent ( QKeyEvent * event)
{
qDebug ( ) << "-----------" << event-> text ( ) ;
qDebug ( ) << "+++++++++++" << event-> key ( ) ;
if ( event-> text ( ) == "w" )
{
lbl-> move ( lbl-> x ( ) , lbl-> y ( ) - 10 ) ;
}
if ( event-> text ( ) == "s" )
{
lbl-> move ( lbl-> x ( ) , lbl-> y ( ) + 10 ) ;
}
if ( event-> text ( ) == "a" )
{
lbl-> move ( lbl-> x ( ) - 10 , lbl-> y ( ) ) ;
}
if ( event-> text ( ) == "d" )
{
lbl-> move ( lbl-> x ( ) + 10 , lbl-> y ( ) ) ;
}
}
void Widget :: mousePressEvent ( QMouseEvent * event)
{
qDebug ( ) << "------mouse press----------" ;
if ( event-> button ( ) == Qt:: LeftButton)
{
ui-> label-> setText ( "left" ) ;
}
if ( event-> button ( ) == Qt:: RightButton)
{
ui-> label-> setText ( "right" ) ;
}
}
void Widget :: mouseReleaseEvent ( QMouseEvent * event)
{
qDebug ( ) << "------mouse release----------" ;
ui-> label-> setText ( "none" ) ;
}
void Widget :: mouseMoveEvent ( QMouseEvent * event)
{
qDebug ( ) << "---------mouse move-----------" ;
ui-> label-> move ( event-> x ( ) - ui-> label-> width ( ) / 2 , event-> y ( ) - ui-> label-> height ( ) / 2 ) ;
}
void Widget :: timerEvent ( QTimerEvent * event)
{
lbl-> move ( lbl-> x ( ) + 10 , lbl-> y ( ) + 10 ) ;
}
bool Widget :: event ( QEvent * event)
{
qDebug ( ) << "0000000000000000000" ;
if ( event-> type ( ) == QEvent:: KeyPress)
{
keyPressEvent ( ( QKeyEvent * ) event) ;
return true ;
}
return QWidget :: event ( event) ;
}
# endif
bool Widget :: eventFilter ( QObject * watched, QEvent * event)
{
qDebug ( ) << "1111111111111111111" ;
if ( watched == ui-> label_2)
{
if ( event-> type ( ) == QEvent:: MouseButtonPress)
{
timeId = startTimer ( 100 ) ;
return true ;
}
}
if ( watched == ui-> label_3)
{
if ( event-> type ( ) == QEvent:: MouseButtonPress)
{
killTimer ( timeId) ;
return true ;
}
}
return QWidget :: eventFilter ( watched, event) ;
}
QPainter
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QPainter>
# include <QPen>
# include <QBrush>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
void paintEvent ( QPaintEvent * event) ;
private :
Ui:: Widget * ui;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
this -> setMinimumSize ( 1920 , 1080 ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: paintEvent ( QPaintEvent * event)
{
QPainter painter ( this ) ;
QPen pen;
pen. setStyle ( Qt:: DashDotDotLine) ;
pen. setColor ( QColor ( "green" ) ) ;
pen. setWidth ( 5 ) ;
painter. setPen ( pen) ;
painter. drawLine ( QPoint ( 0 , 0 ) , QPoint ( 200 , 200 ) ) ;
QBrush brush;
brush. setColor ( QColor ( "black" ) ) ;
brush. setStyle ( Qt:: Dense4Pattern) ;
painter. setBrush ( brush) ;
painter. drawEllipse ( QPoint ( 300 , 300 ) , 100 , 100 ) ;
painter. translate ( 200 , 200 ) ;
painter. drawRect ( 400 , 400 , 100 , 200 ) ;
painter. rotate ( - 45 ) ;
pen. setStyle ( Qt:: DashDotDotLine) ;
pen. setColor ( QColor ( "red" ) ) ;
pen. setWidth ( 5 ) ;
painter. setPen ( pen) ;
painter. drawRect ( 400 , 400 , 100 , 200 ) ;
}
进线程
QThread
# ifndef MYTHREAD_H
# define MYTHREAD_H
# include <QObject>
# include <QThread>
# include <QDebug>
class MyThread : public QThread
{
Q_OBJECT
public :
MyThread ( ) ;
void run ( ) ;
signals:
void valueChange ( int ) ;
public slots:
void quitThread ( ) ;
void exitThread ( ) ;
private :
int value;
bool flag;
} ;
# endif
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QThread>
# include "mythread.h"
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_clicked ( ) ;
void on_pushButton_2_clicked ( ) ;
void on_pushButton_3_clicked ( ) ;
signals:
void quitThread ( ) ;
void exitThread ( ) ;
private :
Ui:: Widget * ui;
MyThread * thread;
} ;
# endif
# include "mythread.h"
MyThread :: MyThread ( )
{
value = 0 ;
flag = false ;
}
void MyThread :: run ( )
{
while ( 1 )
{
if ( flag)
{
break ;
}
value++ ;
if ( value> 100 )
{
value = 0 ;
}
qDebug ( ) << value;
emit valueChange ( value) ;
msleep ( 100 ) ;
}
}
void MyThread :: quitThread ( )
{
flag = true ;
quit ( ) ;
}
void MyThread :: exitThread ( )
{
exit ( ) ;
}
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_pushButton_clicked ( )
{
thread = new MyThread;
connect ( thread, & MyThread:: valueChange, ui-> progressBar, & QProgressBar:: setValue) ;
connect ( thread, & MyThread:: finished, thread, & MyThread:: deleteLater) ;
connect ( this , & Widget:: quitThread, thread, & MyThread:: quitThread) ;
connect ( this , & Widget:: exitThread, thread, & MyThread:: exitThread) ;
thread-> start ( ) ;
}
void Widget :: on_pushButton_2_clicked ( )
{
}
void Widget :: on_pushButton_3_clicked ( )
{
emit quitThread ( ) ;
emit exitThread ( ) ;
}
QMutex
# include "mythread1.h"
# include <QDebug>
Mythread1 :: Mythread1 ( int * buf, int num, QMutex & mutex) : buf ( buf) , num ( num) , mutex ( mutex)
{
}
void Mythread1 :: run ( )
{
mutex. lock ( ) ;
for ( int i= 0 ; i< num; i++ )
{
buf[ i] *= 2 ;
}
for ( int i= 0 ; i< num; i++ )
{
printf ( "%d " , buf[ i] ) ;
}
printf ( "\n" ) ;
mutex. unlock ( ) ;
}
# include "mythread2.h"
Mythread2 :: Mythread2 ( int * buf, int num, QMutex & mutex) : buf ( buf) , num ( num) , mutex ( mutex)
{
}
void Mythread2 :: run ( )
{
mutex. lock ( ) ;
for ( int i= 0 ; i< num; i++ )
{
buf[ i] *= 3 ;
}
for ( int i= 0 ; i< num; i++ )
{
printf ( "%d " , buf[ i] ) ;
}
printf ( "\n" ) ;
mutex. unlock ( ) ;
}
# include <QCoreApplication>
# include "mythread1.h"
# include "mythread2.h"
# include <QMutex>
int main ( int argc, char * argv[ ] )
{
QCoreApplication a ( argc, argv) ;
int buf[ 5 ] = { 1 , 2 , 3 , 4 , 5 } ;
QMutex mutex;
Mythread1 * t1 = new Mythread1 ( buf, 5 , mutex) ;
Mythread2 * t2 = new Mythread2 ( buf, 5 , mutex) ;
t1-> start ( ) ;
t2-> start ( ) ;
QObject :: connect ( t1, & Mythread1:: finished, t1, & Mythread1:: deleteLater) ;
QObject :: connect ( t2, & Mythread2:: finished, t2, & Mythread2:: deleteLater) ;
return a. exec ( ) ;
}
QProcess
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QProcess>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_clicked ( ) ;
private :
Ui:: Widget * ui;
QProcess * process;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
process = new QProcess ( this ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_pushButton_clicked ( )
{
process-> start ( "D:\\tools\\拆分\\分拆器\\PngSplit.exe" ) ;
}
网络
QTcpSocket(客户端)
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QTcpSocket>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_clicked ( ) ;
void netConnected ( ) ;
void showError ( QAbstractSocket:: SocketError) ;
void on_pushButton_2_clicked ( ) ;
void netDisConnected ( ) ;
void on_pushButton_3_clicked ( ) ;
void recv ( ) ;
private :
Ui:: Widget * ui;
QTcpSocket * tcpSocket;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
# include <QMessageBox>
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_pushButton_clicked ( )
{
if ( ui-> lineEdit-> text ( ) . isEmpty ( ) || ui-> lineEdit_2-> text ( ) . isEmpty ( ) )
{
QMessageBox :: warning ( this , "warning" , "please input" ) ;
return ;
}
tcpSocket = new QTcpSocket ( this ) ;
connect ( tcpSocket, & QTcpSocket:: connected, this , & Widget:: netConnected) ;
connect ( tcpSocket, SIGNAL ( error ( QAbstractSocket:: SocketError) ) , this , SLOT ( showError ( QAbstractSocket:: SocketError) ) ) ;
connect ( tcpSocket, & QTcpSocket:: disconnected, this , & Widget:: netDisConnected) ;
connect ( tcpSocket, & QTcpSocket:: readyRead, this , & Widget:: recv) ;
tcpSocket-> connectToHost ( ui-> lineEdit-> text ( ) , ui-> lineEdit_2-> text ( ) . toInt ( ) ) ;
# if 0
if ( tcpSocket-> waitForConnected ( 5 * 1000 ) )
{
ui-> textEdit-> append ( "connect success" ) ;
}
else
{
ui-> textEdit-> append ( QString ( "connect fail:%1" ) . arg ( tcpSocket-> errorString ( ) ) ) ;
}
# endif
}
void Widget :: netConnected ( )
{
qDebug ( ) << "connect" ;
ui-> textEdit-> append ( "connect success" ) ;
}
void Widget :: showError ( QAbstractSocket:: SocketError error)
{
qDebug ( ) << "error" ;
ui-> textEdit-> append ( QString ( "connect fail:%1,%2" ) . arg ( error) . arg ( tcpSocket-> errorString ( ) ) ) ;
}
void Widget :: on_pushButton_2_clicked ( )
{
if ( tcpSocket-> isValid ( ) )
{
tcpSocket-> disconnectFromHost ( ) ;
tcpSocket-> close ( ) ;
}
}
void Widget :: netDisConnected ( )
{
qDebug ( ) << "disconnected" ;
ui-> textEdit-> append ( "disconnected" ) ;
}
void Widget :: on_pushButton_3_clicked ( )
{
if ( tcpSocket-> isValid ( ) )
{
tcpSocket-> write ( ui-> textEdit_2-> toPlainText ( ) . toLatin1 ( ) ) ;
}
}
void Widget :: recv ( )
{
qDebug ( ) << tcpSocket-> bytesAvailable ( ) ;
QByteArray data = tcpSocket-> read ( tcpSocket-> bytesAvailable ( ) ) ;
ui-> textEdit-> append ( "Server:" + data) ;
}
QTcpServer(服务器)
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QTcpServer>
# include <QTcpSocket>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_clicked ( ) ;
void newConnection ( ) ;
void on_pushButton_2_clicked ( ) ;
void recv ( ) ;
void errorShow ( ) ;
void netDisConnect ( ) ;
private :
Ui:: Widget * ui;
QTcpServer * server;
QTcpSocket * socket;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
# include <QMessageBox>
# include <QDebug>
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
server = new QTcpServer ( this ) ;
socket = new QTcpSocket ( this ) ;
connect ( server, & QTcpServer:: newConnection, this , & Widget:: newConnection) ;
ui-> pushButton_2-> setDisabled ( true ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_pushButton_clicked ( )
{
if ( ui-> lineEdit-> text ( ) . isEmpty ( ) )
{
QMessageBox :: warning ( this , "warning" , "port is empty" ) ;
return ;
}
if ( ! server-> listen ( QHostAddress:: Any, ui-> lineEdit-> text ( ) . toInt ( ) ) )
{
ui-> textEdit-> append ( "launch fail" ) ;
}
else
{
ui-> textEdit-> append ( "launch success" ) ;
}
}
void Widget :: newConnection ( )
{
if ( server-> hasPendingConnections ( ) )
{
socket = server-> nextPendingConnection ( ) ;
if ( ! socket-> isValid ( ) )
{
ui-> textEdit-> append ( QString ( "error,%1" ) . arg ( server-> errorString ( ) ) ) ;
return ;
}
ui-> textEdit-> append ( QString ( "client: %1 , %2" ) . arg ( socket-> peerAddress ( ) . toString ( ) ) . arg ( socket-> peerPort ( ) ) ) ;
ui-> pushButton_2-> setDisabled ( false ) ;
connect ( socket, & QTcpSocket:: readyRead, this , & Widget:: recv) ;
connect ( socket, SIGNAL ( error ( QAbstractSocket:: SocketError) ) , this , SLOT ( netDisConnect ( ) ) ) ;
connect ( socket, & QTcpSocket:: disconnected, this , & Widget:: netDisConnect) ;
}
}
void Widget :: on_pushButton_2_clicked ( )
{
socket-> write ( ui-> textEdit_2-> toPlainText ( ) . toLatin1 ( ) ) ;
}
void Widget :: recv ( )
{
ui-> textEdit-> append ( QString ( "client: %1" ) . arg ( QString ( socket-> readAll ( ) ) ) ) ;
}
void Widget :: errorShow ( )
{
ui-> textEdit-> append ( QString ( "error: %1" ) . arg ( socket-> errorString ( ) ) ) ;
}
void Widget :: netDisConnect ( )
{
ui-> pushButton_2-> setDisabled ( true ) ;
disconnect ( socket, & QTcpSocket:: readyRead, this , & Widget:: recv) ;
disconnect ( socket, SIGNAL ( error ( QAbstractSocket:: SocketError) ) , this , SLOT ( netDisConnect ( ) ) ) ;
disconnect ( socket, & QTcpSocket:: disconnected, this , & Widget:: netDisConnect) ;
socket-> close ( ) ;
ui-> textEdit-> append ( "disconneted" ) ;
}
QUdpSocket(主)
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QUdpSocket>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_clicked ( ) ;
void recvData ( ) ;
void on_pushButton_2_clicked ( ) ;
private :
Ui:: Widget * ui;
QUdpSocket * socket;
QHostAddress addr;
quint16 port;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
socket = new QUdpSocket;
connect ( socket, & QUdpSocket:: readyRead, this , & Widget:: recvData) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_pushButton_clicked ( )
{
if ( socket-> bind ( QHostAddress:: Any, ui-> lineEdit-> text ( ) . toInt ( ) ) )
ui-> pushButton-> setDisabled ( true ) ;
}
void Widget :: recvData ( )
{
char buf[ 32 ] = { 0 } ;
socket-> readDatagram ( buf, sizeof ( buf) , & addr, & port) ;
ui-> textEdit-> insertPlainText ( addr. toString ( ) + ":" + QString ( buf) + "\n" ) ;
}
# include <QDebug>
void Widget :: on_pushButton_2_clicked ( )
{
qDebug ( ) << socket-> writeDatagram ( ui-> textEdit_2-> toPlainText ( ) . toLatin1 ( ) ,
addr, port) ;
qDebug ( ) << "size: " << ui-> textEdit_2-> toPlainText ( ) . size ( ) ;
}
QUdpSocket(副)
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QUdpSocket>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_clicked ( ) ;
void recvData ( ) ;
private :
Ui:: Widget * ui;
QUdpSocket * socket;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
socket = new QUdpSocket;
connect ( socket, & QUdpSocket:: readyRead, this , & Widget:: recvData) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_pushButton_clicked ( )
{
socket-> writeDatagram ( ui-> textEdit_2-> toPlainText ( ) . toLatin1 ( ) , QHostAddress ( ui-> lineEdit-> text ( ) ) , ui-> lineEdit_2-> text ( ) . toUInt ( ) ) ;
}
# include <QDebug>
void Widget :: recvData ( )
{
char buf[ 32 ] = { 0 } ;
socket-> readDatagram ( buf, sizeof ( buf) ) ;
ui-> textEdit-> insertPlainText ( QString ( buf) + "\n" ) ;
}
IO操作
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QFile>
# include <QFileDialog>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_pushButton_clicked ( ) ;
void on_pushButton_2_clicked ( ) ;
private :
Ui:: Widget * ui;
QFile file;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
# include <QDebug>
void Widget :: on_pushButton_clicked ( )
{
QString fileName = QFileDialog :: getOpenFileName ( ) ;
if ( fileName. isEmpty ( ) )
return ;
file. setFileName ( fileName) ;
if ( ! file. open ( QFile:: ReadWrite | QFile:: Text) ) {
qDebug ( ) << "文件打开失败" << file. errorString ( ) ;
return ;
}
QByteArray data = file. readAll ( ) ;
qDebug ( ) << "len: " << data. length ( ) ;
ui-> textEdit-> setText ( data) ;
}
void Widget :: on_pushButton_2_clicked ( )
{
QString data = ui-> textEdit-> toPlainText ( ) ;
if ( ! file. isOpen ( ) ) {
QString fileName = QFileDialog :: getSaveFileName ( ) ;
if ( fileName. isEmpty ( ) ) {
qDebug ( ) << "未选择保存的文件名" ;
return ;
}
file. setFileName ( fileName) ;
if ( ! file. open ( QFile:: WriteOnly | QFile:: Text) ) {
qDebug ( ) << "文件打开失败" << file. errorString ( ) ;
return ;
}
}
qDebug ( ) << "size: " << file. size ( ) ;
file. seek ( 0 ) ;
qDebug ( ) << "写入文件:" << file. write ( data. toLatin1 ( ) ) ;
file. flush ( ) ;
file. close ( ) ;
}
数据库
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QSqlDatabase>
# include <QSqlQuery>
# include <QSqlRecord>
# include <QSqlError>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = 0 ) ;
~ Widget ( ) ;
private slots:
void on_btnAdd_clicked ( ) ;
void on_btnSelect_clicked ( ) ;
private :
Ui:: Widget * ui;
QSqlDatabase db;
int row = 0 ;
} ;
# endif
# include "widget.h"
# include "ui_widget.h"
# include <QDebug>
# include <QMessageBox>
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
if ( ! db. contains ( "qt_sql_default_connnection" ) ) {
qDebug ( ) << "句柄不包含 qt_sql_default_connnection 连接" ;
db = QSqlDatabase :: addDatabase ( "QSQLITE" ) ;
db. setDatabaseName ( "my.db" ) ;
} else {
qDebug ( ) << "本地存在 my.db" ;
db = QSqlDatabase :: database ( "qt_sql_default_connnection" ) ;
}
if ( ! db. isOpen ( ) ) {
qDebug ( ) << "数据库未打开" ;
if ( ! db. open ( ) ) {
QMessageBox :: warning ( this , "警告" , "数据打开失败" ) ;
return ;
}
}
QString sql = "create table if not exists user("
"id integer primary key autoincrement, "
"name varchar(12), "
"age int, "
"sex varchar(4));" ;
QSqlQuery query;
query. prepare ( sql) ;
if ( ! query. exec ( ) ) {
qDebug ( ) << "错误:" << query. lastError ( ) . text ( ) ;
QMessageBox :: warning ( this , "警告" , query. lastError ( ) . text ( ) ) ;
}
qDebug ( ) << "数据库表创建成功" ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: on_btnAdd_clicked ( )
{
if ( ui-> edtName-> text ( ) . isEmpty ( ) || ui-> edtAge-> text ( ) . isEmpty ( ) ||
ui-> edtSex-> text ( ) . isEmpty ( ) ) {
QMessageBox :: warning ( this , "警告" , "插入的数据不完整" ) ;
return ;
}
QString sql = QString ( "insert into user values(NULL, '%1', '%2', '%3');" )
. arg ( ui-> edtName-> text ( ) )
. arg ( ui-> edtAge-> text ( ) . toInt ( ) )
. arg ( ui-> edtSex-> text ( ) ) ;
QSqlQuery query;
if ( ! query. exec ( sql) ) {
QMessageBox :: warning ( this , "错误" , query. lastError ( ) . text ( ) ) ;
return ;
}
QMessageBox :: information ( this , "提示" , "数据增加成功" ) ;
}
void Widget :: on_btnSelect_clicked ( )
{
row = 0 ;
ui-> tableWidget-> clear ( ) ;
QStringList list;
list << "编号" << "姓名" << "年龄" << "性别" ;
ui-> tableWidget-> setHorizontalHeaderLabels ( list) ;
QString sql;
QString name = ui-> edtName-> text ( ) ;
if ( name. isEmpty ( ) ) {
sql = "select * from user;" ;
} else {
sql = QString ( "select * from user where name = '%1';" ) . arg ( name) ;
}
QSqlQuery query;
if ( ! query. exec ( sql) ) {
QMessageBox :: warning ( this , "错误" , query. lastError ( ) . text ( ) ) ;
return ;
}
qDebug ( ) << query. record ( ) . count ( ) ;
while ( query. next ( ) ) {
qDebug ( ) << query. value ( "id" ) . toString ( ) << query. value ( "name" ) . toString ( ) ;
ui-> tableWidget-> setRowCount ( row+ 1 ) ;
# if 0
ui-> tableWidget-> setItem ( row, 0 , new QTableWidgetItem ( query. value ( "id" ) . toString ( ) ) ) ;
ui-> tableWidget-> setItem ( row, 1 , new QTableWidgetItem ( query. value ( "name" ) . toString ( ) ) ) ;
ui-> tableWidget-> setItem ( row, 2 , new QTableWidgetItem ( query. value ( "age" ) . toString ( ) ) ) ;
ui-> tableWidget-> setItem ( row, 3 , new QTableWidgetItem ( query. value ( "sex" ) . toString ( ) ) ) ;
# else
for ( int i = 0 ; i < query. record ( ) . count ( ) ; i++ ) {
ui-> tableWidget-> setItem ( row, i, new QTableWidgetItem ( query. value ( i) . toString ( ) ) ) ;
}
# endif
row++ ;
}
}