Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140470
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 265
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-04 13:11
文章分类

全部博文(29)

文章存档

2015年(2)

2014年(27)

我的朋友

分类: 嵌入式

2014-08-25 20:46:06

(一)              第一个Qt程序,helio,Qt

程序源码:

#include

#include

int main(int argc,char* argv[])

{

        QApplication app(argc,argv);

                   QLable *lable = new QLable(“hello,Qt”)

        lable->show();

        return app.exec();

}

程序分析:

1,  创建一个 QApplication 对象。这个对象用于管理应用程序级别的资源,QApplication 的构造函数要求两个参数。

2,  创建一个 QLabel 对象,并且能够显示 Hello, world! 字符串。

3,  将应用程序的控制权移交给 Qt

4,  我们并没有使用 delete 去删除创建的 QLabel,因为在程序结束后操作系统会回收这个空间——这只是因为这个 QLabel 占用的内存比较小,但有时候这么做会引起麻烦的,特别是在大程序中,因此必须小心。

 

(二)              使用按钮响应用户操作

程序源码:

#include

#include

int main(int argc,char* argv[])

{

        QApplication app(argc,argv);

        QPushButton *button = new QPushButton(“quit”);

        QObject::connect(button,SIGNAL(clicked())),&app,SLOT(quit()));

        button->show();

        return app.exec();

}

程序分析:

1,  如果 button 发出clicked信号,你就去执行我的quit() 函数。所以,当我们点击 button 的时候,quit() 函数被调用,程序退出了。所以在这里clicked()就是一个信号,而quit()就是槽,形象地说就是把这个信号插进这个槽里面去。

2,  这个信号槽机制仅仅是使用的QObjectconnect函数。

 

(三)              窗口部件的布局

程序源码:

 

#include

#include

#include

#include

int main(int argc,char* argv[])

{

        QApplication app(argc,argv);

 

        QWidget *window = new QWidget;

        window->setWindowTitle("enter your age");     //设置窗口标题

 

        QSpinBox *spinBox = new QSpinBox;

        QSlider *slinder = new QSlider(Qt::Horizontal);   //设置为水平方向滑动杆

        spinBox->setRange(0,130);    //设置范围

        slinder->setRange(0,130);

 

        QObject::connect(spinBox,SIGNAL(valueChanged(int)),

                slinder,SLOT(setValue(int)));    //使用新值调用setValue(int)函数

        QObject::connect(slinder,SIGNAL(valueChanged(int)),

                spinBox,SLOT(setValue(int)));

        spinBox->setValue(35);

 

        QHBoxLayout *layout = new QHBoxLayout;  //设置一个水平布局管理器

        layout->addWidget(spinBox);

        layout->addWidget(slinder);

        window->setLayout(layout);    //在窗口上安装布局管理器

 

        window->show();

        return app.exec();

}

运行效果:

程序源码:

(1)       头文件FindDialog.h

#ifndef FINDDIALOG_H

#define FINDDIALOG_H

#include

class QCheckBox;

class QLabel;

class QLineEdit;

class QPushButton;

class FindDialog : public QDialog    

{

                Q_OBJECT   //所用定义信号和槽的类,此头文件是必需的

public:

                FindDialog(QWidget *parent = 0); //parent参数指定其父对象,0无父对象

                ~FindDialog();

signals:          //单击find按钮时,可能发射的两个信号,向前查询和向后查询

                void findNext(const QString &str, Qt::CaseSensitivity cs);

                void findPrevious(const QString &str, Qt::CaseSensitivity cs);

private slots:

               void findClicked();

               void enableFindButton(const QString &text);

private:

               QLabel *label;       //显示一个字符串

               QLineEdit *lineEdit;   //单行文本框

               QCheckBox *caseCheckBox;         //带文本的复选框

               QCheckBox *backwardCheckBox;    

               QPushButton *findButton;          //按钮

               QPushButton *closeButton;         

};

#endif // FINDDIALOG_H

 

(2)       源文件FindDialog.cpp

#include

#include "FindDialog.h"

FindDialog::FindDialog(QWidget *parent): QDialog(parent)

{

    label =new QLabel(tr("Find &what:"));    //字符串中的&代表快捷键

    lineEdit =new QLineEdit;

    label->setBuddy(lineEdit);   //按下快捷键时,输入焦点跳到labelbuddylineEdit

    caseCheckBox =new QCheckBox(tr("Match &case"));

    backwardCheckBox =new QCheckBox(tr("Search &backford"));

    findButton =new QPushButton(tr("&Find"));

    findButton->setDefault(true);   //find按钮成为对话框的默认按钮

    findButton->setEnabled(false);  //用来禁用find按钮

    closeButton =new QPushButton(tr("Close"));

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

    connect(findButton, SIGNAL(clicked()),this,SLOT(findClicked()));

connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

 

    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());

}

FindDialog::~FindDialog()  

{

}

void FindDialog::findClicked()    //当点击find时调用

{

    QString text = lineEdit->text();

    Qt::CaseSensitivity cs =caseCheckBox->isChecked()?Qt::CaseInsensitive : Qt::CaseSensitive;

    if(backwardCheckBox->isChecked()) //通过判断Search backford的值

        emit findPrevious(text, cs);

    else

        emit findNext(text, cs);

}

void FindDialog::enableFindButton(const QString &text)  //当文本框变化时调用

{

    findButton->setEnabled(!text.isEmpty());

}

(3)       源文件main.cpp

#include

#include "FindDialog.h"

int main(int argc, char *argv[])

{

    QApplication app(argc, argv);

    FindDialog *dialog =new FindDialog;

    dialog->show();

    return app.exec();

}

程序分析:

1,  QDialogQt中对话框的基类,从QWidget中派生出来。

2,  QCheckBox窗口部件提供一个带文本标签的复选框。

3,  QLineEdit是单行文本框

 

(五)              菜单栏、工具栏、状态栏、标准对话框的编写

(1)       头文件mainwindow.h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include

class QAction;       //前向声明

namespace Ui {

class MainWindow;

}

 

class MainWindow : public QMainWindow

{

    Q_OBJECT

public:

    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();

private     slots:

    void open();

private:

    Ui::MainWindow *ui;

    QAction *openAction;      //添加一个打开命令

};

#endif // MAINWINDOW_H

 

(2)       源文件mainwindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include

#include

#include

#include

#include

#include

#include

MainWindow::MainWindow(QWidget *parent) QMainWindow(parent),ui(new Ui::MainWindow)

{

    ui->setupUi(this);

    openAction = new QAction(tr("&Open"),this);   //含有两个参数,this指明父指针

    openAction->setShortcut(QKeySequence::Open); //设置快捷键

    openAction->setStatusTip(tr("Open a file."));

    openAction->setIcon(QIcon(":/Open.png"));  //setIcon 添加图标。添加的类是QIcon。字符串以 : 开始,后面跟着 prefix,然后图片的路径。

connect(openAction, SIGNAL(triggered()),this,SLOT(open())); //建立一个信号槽

 

    QMenu *file = menuBar()->addMenu(tr("&File"));//action添加到菜单

    file->addAction(openAction);

    QToolBar *toolBar = addToolBar(tr("&File")); //action添加到工具栏

    toolBar->addAction(openAction);

}

void MainWindow::open()

{

    QString path = QFileDialog::getOpenFileName(this,tr("OpenImage"),".",tr("Image Files(*.jpg *.png)"));   //获取打开文件名

    if(path.length() == 0)

    {

        QMessageBox::information(NULL,tr("Path"),tr("You didn't select any files."));  //显示

    }

    else

    {

        QMessageBox::information(NULL,tr("Path"),tr("You selected")+path);

    }

}

MainWindow::~MainWindow()

{

    delete ui;

}

(3)       源文件main.cpp

#include

#include "mainwindow.h"

 

int main(int argc, char *argv[])

{

    QApplication a(argc, argv);

    MainWindow w;

    w.show();

    return a.exec();

}

程序分析:

1QAction类提供了抽象的用户界面action,这些action可以被放置在窗口部件中。一旦QAction被创建了,那么就必须将它添加到相关的菜单和工具栏上,然后将它们链接到实现相应action功能的槽函数上。

2,为了添加图标,我们首先要使用 Qt 的资源文件。完成之后生成一个.qrc文件。首先选择 Add prefix,然后把生成的/new/prefix 改成/。添加过 prefix 之后,然后在工程文件中添加一个图标,再选择 Add file,选择那个图标。这样完成之后保存qrc文件即可。

3QFileDialog Qt中用于文件打开和保存的对话框。QFileDialog 提供了很多静态函数,用于获取用户选择的文件。这里我们使用的是getOpenFileName(),

 

 

阅读(1889) | 评论(0) | 转发(0) |
0

上一篇:引用的概念

下一篇:Qt学习(二)

给主人留下些什么吧!~~