Qt初步的信号与槽

首先瞎说一些废话:
因为Qt的环境很恶心,我从官网上下载花了16个小时还是只完成4%,所以,是不是资源有问题。
接着对于vs2008和vs2010,至今没有看到Qt对于哪个支持的比较好,我的2008和2010对代码辨认轮番好使,真心不知道有什么地方出现问题。

代码提示安装了一个VAssisX的VC插件,包含Qt路径中的include之后,于是,你可以看见犹如Eclipse的强大代码提示功能了。


Qt的消息响应,其实就总结与消息与槽的映射:
connect(sender,SIGNAL(signal),reciver,SLOT(slot));
前两个参数分别是发出消息的对象和消息函数;后面两个参数分别是接受者的对象和响应函数;

例如:
connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));

lineEdit的文字被修改的时候,触发了textChanged()函数,之后窗口类接受消息,通过槽enableFindButton()实现消息响应;

下面是完整的FindDialog的实现:


//find.h


#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QCheckBox>
#include <QtGui/QLineEdit>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog :public QDialog
{
 Q_OBJECT
public:
 FindDialog(QWidget* parent = 0);
signals:
 void findNext(QString &str,Qt::CaseSensitivity cs);
 void findPrevious(QString &str,Qt::CaseSensitivity cs);
 private
slots:
 void findChecked();
 void enableFindButton(const QString &text);
private:
 QLabel *label;
 QLineEdit *lineEdit;
 QCheckBox *caseCheckBox;
 QCheckBox *backwardCheckBox;
 QPushButton *findButton;
 QPushButton *closeButton;
};

#endif

 

//find.cpp
#include "find.h"
#include <QtGui/QtGui>

FindDialog::FindDialog(QWidget* parent):QDialog(parent)
{
 //create label add lineEdit into 
 label = new QLabel(tr("Find &what:"));
 lineEdit = new QLineEdit;
 label->setBuddy(lineEdit);

 //single choose check
 caseCheckBox = new QCheckBox(tr("Mach &case"));
 backwardCheckBox = new QCheckBox(tr("Search &backward"));
 
 //button initialization
 findButton = new QPushButton(tr("&Find"));
 findButton->setDefault(true);
 findButton->setEnabled(false);
 closeButton = new QPushButton(tr("Close"));

 //signal and slot
 //if the textChaged light the find_button
 connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));
 //if findButton be clicked  slot findClick()
 connect(findButton,SIGNAL(clicked()),this,SLOT(fingClicked()));
 //if closeButton be clicked close the dialog
 connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

 //Layout
 QHBoxLayout *topleftLayout = new QHBoxLayout;
 topleftLayout->addWidget(label);
 topleftLayout->addWidget(lineEdit);
 QVBoxLayout *leftLayout = new QVBoxLayout;
 leftLayout->addLayout(topleftLayout);
 leftLayout->addWidget(caseCheckBox);
 leftLayout->addWidget(backwardCheckBox);
 QVBoxLayout *rightLayout = new QVBoxLayout;
 rightLayout->addWidget(findButton);
 rightLayout->addWidget(closeButton);
 rightLayout->addStretch();
 QHBoxLayout* mainLayout = new QHBoxLayout;
 mainLayout->addLayout(leftLayout);
 mainLayout->addLayout(rightLayout);
 setLayout(mainLayout);
 setWindowTitle(tr("Find"));
 setFixedHeight(sizeHint().height());
}

void FindDialog::findChecked()
{
 QString text = lineEdit->text();
 Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;
 if(backwardCheckBox->isChecked()){
  emit findPrevious(text,cs);
 }else{
  emit findNext(text,cs);
 }
}

void FindDialog::enableFindButton(const QString &text){
 findButton->setEnabled(!text.isEmpty());
}

 

//main.cpp

#include "qtry.h"
#include <QtGui/QApplication>
#include "find.h"

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 FindDialog* dialog = new FindDialog();
 dialog->show();
 return a.exec();
}

 


再如:
Qt实现简易时钟,painter实现;
connect(timer,SIGNAL(timeout()),this,SLOT(update()));

当timer发生timerout()消息时,窗口获取消息,通过update()实现。
另外,还知道了painter的setbackground好像不兼容与Qt4,不知道应该怎么解决。


//clock.h
#include <QtGui/QWidget>


class clock:public QWidget
{
 Q_OBJECT
public:
 clock(QWidget *parent = 0);
protected:
 void paintEvent(QPaintEvent *event);
};


//clock.cpp
#include <QtGui/QtGui>
#include "Clock.h"

clock::clock(QWidget* parent):QWidget(parent)
{
 //new clock class

 //new time now.
 QTimer* timer = new QTimer(this);
 //create signal and slot
 connect(timer,SIGNAL(timeout()),this,SLOT(update()));
 //time start between 100ms
 timer->start(100);
 //set window title
 setWindowTitle("Clock");
 //resize the window
 resize(200,200);
 
}

void clock::paintEvent(QPaintEvent *)
{
//time hand set point
 static const QPoint hourhand[3]={
  QPoint(7,8),
  QPoint(-7,8),
  QPoint(0,40)
 };
 static const QPoint minutehand[3]={
  QPoint(7,8),
  QPoint(-7,8),
  QPoint(0,-70)
 };
 static const QPoint secondhand[3]={
  QPoint(7,8),
  QPoint(-7,8),
  QPoint(0,-70)
 };


//set color for handColor
 QColor hourColor(127,0,127);
 QColor minuteColor(0,127,127);
 QColor secondColor(127,127,0);
 int side = qMin(width(),height());

//currentTime for time
 QTime time = QTime::currentTime();

//create painter
 QPainter painter(this);
 painter.setRenderHint(QPainter::Antialiasing);
 painter.translate(width()/2,height()/2);
 painter.scale(side/200.00,side/200.00);

 painter.setPen(Qt::NoPen);
 painter.setBrush(hourColor);

 painter.save();
 painter.rotate(30.0*(time.hour()+time.minute()/60.0));
 painter.drawConvexPolygon(hourhand,3);
 painter.restore();


 painter.setPen(Qt::NoPen);
 painter.setBrush(minuteColor);

 painter.save();
 painter.rotate(6.0*(time.minute()+time.second()/60.0));
 painter.drawConvexPolygon(minutehand,3);
 painter.restore();

 painter.rotate(6.0*time.second());
 painter.drawConvexPolygon(secondhand,3);
 //painter.restore();
 const QColor aaacolor(122,122,122);
 //painter.setBackgroundColor(aaacolor);
 //this->backgroundColor(122,122,122);
}


//main.cpp

#include "tryqt.h"
#include <QtGui/QApplication>
#include <QtGui/QSlider>
#include <QtGui/QWidget>
#include <QtGui/QSpinBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include "Clock.h"

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 clock c;
 c.show();
 return a.exec();

}


代码内部有我很水的英文注释,代码是直接从VS2010中沾过来的,运行没问题。
明天开始学习QtDesigner。加油吧孩子。为了很多。

posted @ 2011-10-16 08:36  chengzheqiao  阅读(2545)  评论(0编辑  收藏  举报