您的位置:首页 > 编程语言 > Qt开发

【Qt5开发及实例】1、启动Qt计划

2014-12-15 18:04 567 查看
1、首先画一个界面,不要问我怎么画的。这里使用的是Qt5,然后用的是Qt creator



项目名字是:Dialog,生成项目



然后就是各种代码:

从上到下依次是:

#-------------------------------------------------
#
# Project created by QtCreator 2014-12-15T16:44:07
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Dialog
TEMPLATE = app

SOURCES += main.cpp\
        dialog.cpp

HEADERS  += dialog.h

FORMS    += dialog.ui


这个是头文件dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
  class Dialog;
}

class Dialog : public QDialog
{
  Q_OBJECT

public:
  explicit Dialog(QWidget *parent = 0);
  ~Dialog();

private slots:
  void on_countBtn_clicked();

  void on_radiuslineEdit_textChanged(const QString &arg1);

private:
  Ui::Dialog *ui;
};

#endif // DIALOG_H


dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

const static double PI = 3.1415926;

Dialog::Dialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::Dialog)
{
  ui->setupUi(this);
}

Dialog::~Dialog()
{
  delete ui;
}

void Dialog::on_countBtn_clicked()
{
    bool ok;
    QString tempStr;
    QString vauleStr = ui->radiuslineEdit->text();  //得到输入的字符串
    int valueInt = vauleStr.toInt(&ok); //这个ok是几个意思??,答案在下面那个槽里面
    double area = valueInt * valueInt * PI; //计算圆的面积,这个PI又是哪里来的??,见程序开头= =
    //ui->areaLabel_2->setText(tempStr.setNum(area)); //把值显示出来
    ui->areaLabel_2->setText(tempStr.setNum(area));
}

void Dialog::on_radiuslineEdit_textChanged(const QString &arg1)
{
    bool ok;
    QString tempStr;
    QString valueStr = ui->radiuslineEdit->text();  //得到这个框里面的文本
    int valueInt = valueStr.toInt(&ok); //这里有个bool类型,默认是0,也就false,不知道有什么用,里面就是bool ok = 0
    double area = valueInt * valueInt * PI; //就算面积
    ui->areaLabel_2->setText(tempStr.setNum(area));
}


mian.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Dialog w;
  w.show();

  return a.exec();
}


dialog.ui 这个是自动生成的

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QLabel" name="radiusLabel">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>90</y>
     <width>54</width>
     <height>12</height>
    </rect>
   </property>
   <property name="text">
    <string>半径:</string>
   </property>
  </widget>
  <widget class="QLabel" name="areaLabel_1">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>150</y>
     <width>54</width>
     <height>12</height>
    </rect>
   </property>
   <property name="text">
    <string>面积:</string>
   </property>
  </widget>
  <widget class="QLabel" name="areaLabel_2">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>150</y>
     <width>111</width>
     <height>16</height>
    </rect>
   </property>
   <property name="frameShape">
    <enum>QFrame::Panel</enum>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Sunken</enum>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QLineEdit" name="radiuslineEdit">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>90</y>
     <width>113</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="countBtn">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>200</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>计算</string>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


项目二,用手写的实现上面那个,也就是不用画界面

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

class Dialog : public QDialog
{
  Q_OBJECT

public:
  Dialog(QWidget *parent = 0);
  ~Dialog();
private:
  QLabel *label1, *label2;  //两个标签框
  QLineEdit *lineEdit;  //文本输入框
  QPushButton *button;  //一个按钮
private slots:
  void showArea();  //显示面积的函数

};

#endif // DIALOG_H


dialog.cpp

#include "dialog.h"
#include <QGridLayout>

const static double PI = 3.1416;

Dialog::Dialog(QWidget *parent)
  : QDialog(parent)
{
  label1 = new QLabel(this);
  label1->setText(tr("请输入圆半径: "));
  lineEdit = new QLineEdit(this);
  label2 = new QLabel(this);
  button = new QPushButton(this);
  button->setText(tr("显示对应圆的面积"));

  QGridLayout *mainLayout = new QGridLayout(this);  //这玩意叫布局管理器
  mainLayout->addWidget(label1, 0, 0);
  mainLayout->addWidget(lineEdit, 0, 1);
  mainLayout->addWidget(label2, 1, 0);
  mainLayout->addWidget(button, 1, 1);

  //来个连接
  connect(button, SIGNAL(clicked()), this, SLOT(showArea())); //把按钮和这个函数关联起来
  //同上
  connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(showArea()));

}

void Dialog::showArea()
{
  bool ok;
  QString tempStr;
  QString valueStr = lineEdit->text();  //得到框中的文本
  int valueInt = valueStr.toInt(&ok);
  double area = valueInt * valueInt * PI;
  label2->setText(tempStr.setNum(area));

}

Dialog::~Dialog()
{

}


mian.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Dialog w;
  w.show();

  return a.exec();
}
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// . ' \\| |// `.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
//
// .............................................
// 佛祖镇楼 BUG辟易
// 佛曰:
// 写字楼里写字间,写字间里程序员;
// 程序人员写程序,又拿程序换酒钱。
// 酒醒只在网上坐,酒醉还来网下眠;
// 酒醉酒醒日复日,网上网下年复年。
// 但愿老死电脑间,不愿鞠躬老板前;
// 奔驰宝马贵者趣,公交自行程序员。
// 别人笑我忒疯癫,我笑自己命太贱;
// 不见满街漂亮妹,哪个归得程序员?


那些年,掉渣天的注释啊!!!!!!

实现展示:



就是这样ok
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: