Object-oriented programming QT- QT, QT Designer

An object-oriented programming

"Case" button to update the time displayed by a display format xx: xx: xx

//主函数文件

#include<QApplication>
#include"TimeDialog.h"
int main(int argc,char** argv){
    
    QApplication app(argc,argv);
    TimeDialog time;
    time.show();
    return app.exec();
}

 

 
// class declaration file 
#ifndef __TIMEDIALOG__
 #define __TIMEDIALOG__ 
#include <QDialog> 
#include <QTime> 
#include <the QPushButton> 
#include <the QLabel> 
#include <QVBoxLayout,> // vertical layout is 
class TimeDialog: public QDialog { 
    the Q_OBJECT 
public : 
    TimeDialog ( void );
 Private slots:
     void timeClicked ( void ); // as the text label assembly grooves function requires one parameter, but no parameters buttonClicked signal. Not match, the custom function groove connection 
Private : 
    
    the QPushButton* m_btnTime;
    QLabel* m_labTime;
   
};

#endif

 

 
// class implementation file 
#include " TimeDialog.h " 
TimeDialog :: TimeDialog () { 
    setWindowTitle ( " Time " );
     // Get button object 
    m_btnTime = new new the QPushButton ( " acquisition time " , the this );
     // Get label object 
    m_labTime = new new QLabel ( " 00:00:00 " , the this ); 
    m_labTime -> setFrameStyle (QFrame Panel :: :: || QFrame Sunken); // set the border style 
    m_labTime-> setAlignment (Qt :: AlignHCenter ||
                :: AlignVCenter qt); // Sets the alignment
     // create a vertical layout is 
    QVBoxLayout, * layout = new new QVBoxLayout, ( the this ); 
    layout -> addWidget (m_labTime); 
    layout -> addWidget (m_btnTime); 
    
    // display a layout 
    setLayout ( layout); 

    // connection 
    connect (m_btnTime, the SIGNAL (clicked ()), the this , the SLOT (timeClicked ())); 
   

} 
void TimeDialog :: timeClicked ( void ) {
     // currentTime QTime returns the class object, he is a static force
     // state member functions
     // toString talk time into a string
    m_labTime->setText(
            QTime::currentTime().
                toString("hh:mm:ss"));
}

 

 You may also be defined from the connection completion signal as a function of the tag (note that only custom function declaration, not defined)

 

 

 

 


 

 

 Two, QT designer

1, "Case" Using the designer reconstruction addition Calculator

(1) Create a project directory and enter

(2) into the input terminal designer interface designers

   Select a parent window template, Dialog without buttons-> click Create

  Widget box (box widget) contains common components, respectively, from top to bottom

The first group (Layouts) Placer
The second group (Spacers) Spreader
The third group (Buttons) Push button
Group IV (Item views) List
The fifth group (Item widgets) List
Group VI (Container) container
Group VII (Input Widgets) Input Components
Group VIII (Display Widgets) Display assembly

  On the right is more important

 (3) Design UI

  1) Drag a Button component, to the dialog, the property calculator button day2 "=" sign.

  Enable Disabled -> Modify objectname is m_btnCalc-> Text text to "=" -> Change font size, point size 18, bold

   2) a drag assembly LineEdit, modify the object named m_editX, modify the font size, thickness. Select the right-justified. We have the right kind LineEdit target operand.

  3) LineEdit result of the object to be read-only.

  4) Label added to the +

(4) single blank space, click the button in the horizontal layout shortcut menu, or select the right layout

(5) Modify the main dialog box titled "Calculator", modify the object name

 (6) Save UI 

 

 (7) to convert the file into C ++ UI support .h file, use the command uic conversion, such as: uic CalculatorDialog.ui -o ui_CalculatorDialog.h // Makefile can accomplish this function, open view its content

   This custom class in two ways:

    1) inherited by other classes  

  The above-described header file of the UI calculator namespace class inherited by the class yesterday calculator, delete the original UI initialization, reservation slot function, connection function, etc., to be completed after the same calculator function

    2) As a class member

   Complete on their own

 
2 "case" login box

 

(1) which is provided according to the required properties in FIG emphasized here two UI design: 1) adjusting the LineEdit echoMode, the input data is not visible. 2) adjusting the minimum and maximum size width and height (the same as the current size), so that non-stretchable

 (2) uic to convert the file header ui 

(3) write code

Write header files inherit LoginDialog or ui_LoginDialog class with the same name as the former can, because there is a namespace called UI in the header file conversion. You can view their relationship.

Writing class implementation file, you need to call setupUI function in a subclass, initialize UI, while the text in the file button changed to Chinese OK and Cancel (using the button () function to get pointer two buttons buttonBox of its parameters is an enumerated value, for example: QButtonBox :: Ok, then use the text provided setText)

To achieve the main file.

(4) build the project, debugging code

 //类声明文件
#ifndef __LOGINDIALOG__
#define __LOGINDIALOG__


#include<QDialog>
#include<QMessageBox>
#include "ui_LoginDialog.h"


class LoginDialog:public QDialog,public Ui::LoginDialog{
  Q_OBJECT
public:
  LoginDialog();

private slots:
  void onAccepted();//单击组合按钮确定,发送的是accepted信号
  void onRejected();//单击组合按钮取消,发送的是rejected信号
};

#endif

 

 //类实现文件
#include "LoginDialog.h"
#include<QPushButton>
LoginDialog::LoginDialog()
{
  //初始化界面
  setupUi(this);
  m_btnBox->button(QDialogButtonBox::Ok)->setText("确定");
  m_btnBox->button(QDialogButtonBox::Cancel)->setText("取消");


  connect(m_btnBox,SIGNAL(accepted()),this,SLOT(onAccepted()));
  connect(m_btnBox,SIGNAL(rejected()),this,SLOT(onRejected()));
}

void LoginDialog::onAccepted(){
  //如果输入正确用户名和密码,则登录成功,关闭父窗口
  //输入错误,弹出提示框
  if(m_editUsername->text()=="tarena"&&m_editPasswd->text()=="123456"){
    qDebug("登陆成功");
    close();

}
else{

  QMessageBox msgBox(QMessageBox::Critical,//提示框风格
    windowTitle(),//标题
    "用户名或密码错误",//提示消息
    QMessageBox::Ok,//选择按钮
    this);//父窗口指针

  msgBox.setButtonText(QMessageBox::Ok,"确定");
  msgBox.exec();
}



}

void LoginDialog::onRejected(){
//创建消息提示狂
  QMessageBox msgBox(QMessageBox::Question,//提示框风格
    windowTitle(),//标题
    "确定取消登录吗?",//提示消息
    QMessageBox::No|QMessageBox::Yes,//选择按钮
    this);//父窗口指针
  msgBox.setButtonText(QMessageBox::Yes,"确定");
  msgBox.setButtonText(QMessageBox::No,"取消");
  //点击yes退出父窗口
  if(msgBox.exec()==QMessageBox::Yes){
    close();
}
}

 

 

//主文件

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

int main(int argc,char** argv){

  QApplication app(argc,argv);

  LoginDialog login;
  login.show();

  return app.exec();

}

 

三、创造器(qtcreator)

1、《案例》重构前述计算器

(1)终端启动创造器

(2)新建工程,选择Application->在选择Qt widgets Application,创建

(3)输入工程名称,指定创建路径,并且会在此路径下创建一个与工程名相同的目录

(4)设置开发套件,采用默认即可 

(5)设置一个类名(这里设置为CalculatorDialog),并选择一个父窗口(基类)

  

注意:

  这一步骤完成后,会自动创建与输入类名对用的头文件,源文件和ui文件,项目文件及项目配置文件

 

(4)下一步,选择项目管理工具,这里没有,跳过。完成

注意:

  在右侧的影子构建勾选后,会将生成的中间文件存放到设置的文件夹下,如图:

  如果比较简单的工程,可去掉该选项

 

 (5)双击ui文件,进入设计。按前述计算机昨天的计算机设置组件属性

注意:

  ui设计时父窗口对象名不能修改

 (6)全选组件  ,再水平布局 

(7)右键=号,选择转到槽,可自动生成槽函数声明和定义,手动添加按键使能槽函数(由EditLine内容来使能),并完成连接,只需完成定义内容即可

 

Guess you like

Origin www.cnblogs.com/ptfe/p/11318695.html