Qt学习

一自定义槽和信号:
槽的定义与声明要分开,头文件放声明,.cpp文件放槽函数;而自定义信号一般情况下只需给出信号的声明即可,不需要实现,只需用emit关键字发射信号即可。
自定义槽和信号的关键字是Q_OBJECT;只要是自己定义的槽或者信号在声明中前面都要加上。
声明槽用public slots:;声明信号用signals:。
注意:编译自定义的信号和槽时必须用qmake编译。
 
例1自定义槽,实现打印功能。
//4-4.h文件
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
class MyMainWindow : public QWidget
{
              Q_OBJECT;   //关键字
       public:
              MyMainWindow();
              QPushButton *helloworld;
       public slots:
              void Pstring();   //声明槽函数
};
 //4-4.cpp文件
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <4-4.h>    //类的声明头文件
#include <iostream.h>
 
void MyMainWindow::Pstring() //自定义的槽函数
{
       cout<<"Quit!"<<endl;
}
 
MyMainWindow::MyMainWindow()
{
       setGeometry(100,100,200,120);
      
       helloworld= new QPushButton("quit",this);
       helloworld->setGeometry(20,20,160,50);
       helloworld->setFont(QFont("Times",18,QFont::Bold));
      
       connect(helloworld,SIGNAL(clicked()),this,SLOT(Pstring()));
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
运行结果:
当点击quit时,屏幕打印Quit
 
例2自定义信号。
//4-5.h文件
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
class MyMainWindow : public QWidget
{
              Q_OBJECT;
       public:
              MyMainWindow();
              QPushButton *helloworld;
       public slots:
              void Pstring();
       signals:
              void quit();
};
 
//4-5.cpp文件
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <4-4.h>
#include <iostream.h>
 
void MyMainWindow::Pstring()
{
       cout<<"Quit!"<<endl;
}
 
MyMainWindow::MyMainWindow()
{
       setGeometry(100,100,200,120);
      
       helloworld= new QPushButton("quit",this);//this is w
       helloworld->setGeometry(20,20,160,50);
       helloworld->setFont(QFont("Times",18,QFont::Bold));
      
       connect(this,SIGNAL(quit()),this,SLOT(Pstring()));//自定义的信号链接到自定义的槽上
       emit quit();  //连接信号和槽之后,发射信号函数
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
运行结果:
只打印一个字符串Quit
 
例3信号和信号的连接
//4-6.h文件
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
class MyMainWindow : public QWidget
{
              Q_OBJECT;
       public:
              MyMainWindow();
              QPushButton *helloworld;
       public slots:
              void Pstring();
       signals:
              void quit();
};
 
//4-6.cpp文件
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <4-4.h>
#include <iostream.h>
 
void MyMainWindow::Pstring()
{
       cout<<"Quit!"<<endl;
}
 
MyMainWindow::MyMainWindow()
{
       setGeometry(100,100,200,120);
      
       helloworld= new QPushButton("quit",this);//this is w
       helloworld->setGeometry(20,20,160,50);
       helloworld->setFont(QFont("Times",18,QFont::Bold));
      
       connect(helloworld,SIGNAL(clicked()),this,SIGNAL(quit()));//自定义的信号和已知信号连接
connect(this,SIGNAL(quit()),this,SLOT(Pstring()));
       emit quit();
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
运行结果:
和例1一样的结果。
 
 二绘制简单的图形
绘图可以看成是一个绘图事件(paintEvent),类似的还有鼠标事件。所有的绘图操作都必须放在绘图事件中去完成,不需要自己调用,系统会自己调用。
 
例1画一个矩形
#include <qapplication.h>
#include <qwidget.h>
#include <qpainter.h>
 
class MyMainWindow:public QWidget
{
       public:
              MyMainWindow();
       private:
              void paintEvent(QPaintEvent*);
              QPainter *paint;
};
 
void MyMainWindow::paintEvent(QPaintEvent*)
{
       paint=new QPainter;
       paint->begin(this);
       paint->drawRect(20,20,160,160);
       paint->end();
}
 
MyMainWindow::MyMainWindow()
{
       setGeometry(100,100,200,200);
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
 
 例2使用画刷填充颜色
#include <qapplication.h>
#include <qwidget.h>
#include <qpainter.h>
 
class MyMainWindow:public QWidget
{
       public:
              MyMainWindow();
       private:
              void paintEvent(QPaintEvent*);
              QPainter *paint;
};
 
void MyMainWindow::paintEvent(QPaintEvent*)
{
       paint=new QPainter;
       paint->begin(this);
       paint->setPen(QPen(blue,4,QPen::DashLine));//设置画笔和画刷要在画矩形之前
       paint->setBrush(QBrush(red,SolidPattern));
       paint->drawRect(20,20,160,160);
       paint->end();
}
 
MyMainWindow::MyMainWindow()
{
       setGeometry(100,100,200,200);
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
 
 例3 制作简易黑白棋
#include <qapplication.h>
#include <qwidget.h>
#include <qpainter.h>
 
class MyMainWindow:public QWidget
{
       public:
              MyMainWindow();
       private:
              void paintEvent(QPaintEvent*);
              QPainter *paint;
};
 
void MyMainWindow::paintEvent(QPaintEvent*)
{
       int i;
       paint=new QPainter;
       paint->begin(this);
      
       paint->setPen(QPen(black,4,QPen::SolidLine));
       paint->setBrush(QBrush(green,SolidPattern));
       paint->drawRect(20,20,160,160);
      
       paint->setPen(QPen(black,2,QPen::SolidLine));
       for(i=2;i<=8;i++)
       {
              paint->drawLine(20*i,20,20*i,180);
              paint->drawLine(20,20*i,180,20*i);
       }
      
       paint->setBrush(QBrush(blue,SolidPattern));
       paint->drawEllipse(80,80,20,20);
       paint->drawEllipse(100,100,20,20);
      
       paint->setBrush(QBrush(red,SolidPattern));
       paint->drawEllipse(100,80,20,20);
       paint->drawEllipse(80,100,20,20);
       //paint->drawPolygon(30,2,0);
      
       paint->end();
}
 
MyMainWindow::MyMainWindow()
{
       setGeometry(100,100,200,200);
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
 
 
下午内容:
 
一 安装QT4.3.5版本
(1)解压安装包:
# tar �Czxvf qt-x11.tar.gz
(2)配置并安装:
# ./configure  �Cno-openssl
# make
#make install
(3)添加环境变量:
#PATH=/usr/local/Trolltech/Qt-4.3.5/bin:$PATH
 
二 动画类(QMovie)
QT提供QMovie类,用于显示动画(电影)。当前,QMovie只能处理gif格式的动态图片。
 
例1 创建动画
#include <qapplication.h>
#include <qwidget.h>
#include <qlabel.h>
#include <qmovie.h>
class MyMainWindow:public QWidget
{
       public:
              MyMainWindow();
       private:
              QLabel *label;
};
 
MyMainWindow::MyMainWindow()
{
       setGeometry(100,100,140,80);
       QMovie movie("0019.gif");
      
       label=new QLabel(this);
       label->setGeometry(10,10,120,60);
       label->setMovie(movie);
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
 
 例2 自己编写程序,当点击pause时动画暂停,点击continue时动画继续,点击speed时速度加快。
//头文件
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qmovie.h>
#include <qlabel.h>
class MyMainWindow:public QWidget
{
              Q_OBJECT;
       public:
              MyMainWindow();
              QMovie *movie;
       private:
              QLabel *label;
              QPushButton *pause;
              QPushButton *contin;
              QPushButton *speed;
       public slots:
              void Pause();
              void Continue();
              void Speed();
};
 
//主函数
#include <qapplication.h>
#include <qwidget.h>
#include <qlabel.h>
#include <qmovie.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <4-4.h>
#include <iostream.h>
void MyMainWindow::Pause()
{
       movie->pause();
}
 
void MyMainWindow::Continue()
{
       movie->unpause();
}
 
void MyMainWindow::Speed()
{
       movie->setSpeed(300);
}
 
MyMainWindow::MyMainWindow()
{
       setGeometry(20,20,140,250);
       movie=new  QMovie("0019.gif");
      
       label=new QLabel(this);
       label->setGeometry(30,30,120,100);
       label->setMovie(*movie);
      
       pause= new QPushButton("pause",this);
       pause->setGeometry(10,130,120,30);
       pause->setFont(QFont("Times",10,QFont::Bold));
      
       contin=new QPushButton("continue",this);
       contin->setGeometry(10,170,120,30);
       contin->setFont(QFont("Times",10,QFont::Bold));
      
       speed= new QPushButton("speed",this);
       speed->setGeometry(10,210,120,30);
       speed->setFont(QFont("Times",10,QFont::Bold));
      
       connect(pause,SIGNAL(clicked()),this,SLOT(Pause()));
      
       connect(contin,SIGNAL(clicked()),this,SLOT(Continue()));
      
       connect(speed,SIGNAL(clicked()),this,SLOT(Speed()));
 
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
 
 
三 布局管理器
当我们通过QWidget::setGeometry()函数传递绝对坐标的方法来放置QT对象。使用这种方法存在的问题是,当调整父窗口尺寸时,它可能变得太大或者太小而不适合其中的子部件。这时就需要布局管理器。
 
例1 使用布局管理器
 
//头文件
#include <qwidget.h>
#include <qpushbutton.h>
#include <qmovie.h>
#include <qlabel.h>
#include <qlayout.h>
class MyMainWindow:public QWidget
{
              Q_OBJECT;
       public:
              MyMainWindow();
              QMovie *movie;
       private:
              QLabel *label;
              QPushButton *pause;
              QPushButton *contin;
              QPushButton *fast;
              QPushButton *normal;
              QPushButton *slow;
              QVBoxLayout *vbox;
       public slots:
              void Pause();
              void Continue();
              void Fast();
              void Slow();
              void Normal();
};
 
//主函数
#include <qapplication.h>
#include <qwidget.h>
#include <qlabel.h>
#include <qmovie.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <4-4.h>
#include <qlayout.h>
static int i=100;
void MyMainWindow::Pause()
{
       movie->pause();
}
 
void MyMainWindow::Continue()
{
       movie->unpause();
}
 
void MyMainWindow::Fast()
{
       movie->setSpeed(i+=100);
}
 
void MyMainWindow::Normal()
{
       movie->setSpeed(100);
       i=100;
}
 
void MyMainWindow::Slow()
{
       movie->setSpeed(i-=100);
}
 
MyMainWindow::MyMainWindow()
{
       setGeometry(20,20,140,330);
       movie=new  QMovie("0019.gif");
      
       label=new QLabel(this);
       label->setGeometry(30,30,120,100);
       label->setMovie(*movie);
      
       pause= new QPushButton("pause",this);
       pause->setGeometry(10,130,120,30);
       pause->setFont(QFont("Times",10,QFont::Bold));
      
       contin=new QPushButton("continue",this);
       contin->setGeometry(10,170,120,30);
       contin->setFont(QFont("Times",10,QFont::Bold));
      
       fast= new QPushButton("fast",this);
       fast->setGeometry(10,210,120,30);
       fast->setFont(QFont("Times",10,QFont::Bold));
      
       normal= new QPushButton("normal",this);
       normal->setGeometry(10,250,120,30);
       normal->setFont(QFont("Times",10,QFont::Bold));
      
       slow= new QPushButton("slow",this);
       slow->setGeometry(10,290,120,30);
       slow->setFont(QFont("Times",10,QFont::Bold));
      
       vbox=new QVBoxLayout(this);
       vbox->addWidget(label);
       vbox->addWidget(pause);
       vbox->addWidget(contin);
       vbox->addWidget(fast);
       vbox->addWidget(normal);
       vbox->addWidget(slow);
      
       connect(pause,SIGNAL(clicked()),this,SLOT(Pause()));
      
       connect(contin,SIGNAL(clicked()),this,SLOT(Continue()));
      
       connect(fast,SIGNAL(clicked()),this,SLOT(Fast()));
 
       connect(normal,SIGNAL(clicked()),this,SLOT(Normal()));
      
       connect(slow,SIGNAL(clicked()),this,SLOT(Slow()));
}
 
int main(int argc,char **argv)
{
       QApplication a(argc,argv);
       MyMainWindow w;
       a.setMainWidget(&w);
       w.show();
       return a.exec();
}
 

你可能感兴趣的:(职场,qt,休闲)