欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 9.28每日作业

9.28每日作业

2024/10/25 9:26:55 来源:https://blog.csdn.net/qq_62099195/article/details/142620255  浏览:    关键词:9.28每日作业

1> 创建一个新项目,将默认提供的程序都注释上意义

01Demo.pro

QT       += core gui
# QT表示要引入的类库  core:核心库例如IO操作在该库中   gui:图形化界面库
# 如果要使用其他类库中的相关函数,则需要加对于的类库后,才能使用
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# QT超过版本4时,会自动加上 widgetsCONFIG += c++11
# 支持C++11新特性# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0# 实现工程项目的管理
# 管理源文件SOURCES += \main.cpp \mywindow.cpp# 管理头文件HEADERS += \mywindow.h# 管理所有ui文件FORMS += \mywindow.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mywindow.h

#ifndef MYWINDOW_H
#define MYWINDOW_H
//防止头文件重复包含#include <QWidget>//ui_mywnd.h中的命名空间的声明
QT_BEGIN_NAMESPACE
namespace Ui { class MyWindow; }       //将其他文件中的命名空间进行声明
QT_END_NAMESPACE//自定义的类的声明,公共继承自QWidget:QWidget中封装了有关图形化界面的相关操作的具体实现
//由于继承的是系统提供的类,那么自定义的类中即使没有写任何东西,其类中也有很多成员了class MyWindow : public QWidget
{Q_OBJECT        //信号与槽的元对象,直接写,没有该宏,就不能使用信号与槽public:MyWindow(QWidget *parent = nullptr);        //构造函数的声明~MyWindow();                                //析构函数的声明   虚析构函数private:Ui::MyWindow *ui;  //成员属性,指针
};#endif // MYWINDOW_H

mywindow.cpp 

#include "mywindow.h"
#include "ui_mywindow.h"//构造函数的定义
MyWindow::MyWindow(QWidget *parent): QWidget(parent)            //在子类的初始化列表中显式调用父类的有参构造,来完成对子类从父类中继承下来成员的初始化, ui(new Ui::MyWindow)       //给自己的类中的指针成员实例化空间
{ui->setupUi(this);           //将ui界面上拖拽的组件展示到this界面上
}//析构寒湖是的定义
MyWindow::~MyWindow()
{delete ui;                   //释放ui界面申请的组件空间
}

main.cpp

#include "mywindow.h"
//文件包含,自定义的头文件,该头文件中包含了图形化界面类#include <QApplication>
//包含应用程序的头文件int main(int argc, char *argv[])
{QApplication a(argc, argv);    //使用应用程序类实例化对象,调用有参构造MyWindow w;                    //使用自定义的类调用无参构造在栈区构造一个界面对象w.show();                      //调用对象的成员函数,将界面展示出来return a.exec();//a.exec():使用应用程序类对象,调用应用程序的成员函数,保证界面不被关闭,轮询等待界面上的时间发生//等待用户操作界面上的组件//等待界面上的信号与槽的响应//等待事件处理机制的实现
}

2> 使用代码的形式实现登录框

01text.pro

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.h# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();
};
#endif // WIDGET_H

 main.c

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

widget.cpp

#include "widget.h"Widget::Widget(QWidget *parent): QWidget(parent)
{this->resize(900,800);                        //重置界面尺寸this->setWindowTitle("登录");                 //设置界面名称为:登录this->setWindowIcon(QIcon("C:\\Users\\86183\\Desktop\\icon_dj40cz9xze4\\bianji-nan.png"));//设置界面图标QLabel *lab1 = new QLabel(this);              //定义文本lab1->resize(300,200);                        //设置文本框大小lab1->move(300,200);                          //移动文本位置lab1->setPixmap(QPixmap("C:\\Users\\86183\\OneDrive\\图片\\jiguang.jpg"));//设置文本为图片lab1->setScaledContents(true);                //设置图片自动缩放QLabel *lab2 = new QLabel("账号:",this);      //定义文本lab2->resize(50,40);                          //重置文本框大小lab2->move(lab1->x(),lab1->y()+lab1->height()+10);  //移动文本位置QLabel *lab3 = new QLabel("密码:",this);       //定义文本lab3->resize(50,40);                          //重置文本框大小lab3->move(lab2->x(),lab2->y()+lab2->height());    //移动文本位置QLineEdit *edit1 = new QLineEdit(this);edit1->resize(250,30);                      //重置尺寸edit1->move(lab2->x()+lab2->width(),lab2->y());edit1->setStyleSheet("color:black");        //设置字体颜色edit1->setAlignment(Qt::AlignCenter);       //设置文本对齐方式:居中edit1->setPlaceholderText("输入QQ账号");     //设置占位文本QLineEdit *edit2 = new QLineEdit(this);edit2->resize(250,30);                      //重置尺寸edit2->move(lab3->x()+lab3->width(),lab3->y());edit2->setStyleSheet("color:black");         //设置字体颜色edit2->setAlignment(Qt::AlignCenter);       //设置文本对齐方式:居中edit2->setEchoMode(QLineEdit::Password);    //设置密文模式edit2->setPlaceholderText("输入QQ密码");     //设置占位文本QPushButton *btn1 = new QPushButton("登录",this);     //定义按键btn1->resize(110,40);                                //重置按键大小btn1->move(lab3->x(),lab3->y()+lab3->height());      //移动按键位置QPushButton *btn2 = new QPushButton("取消",this);     //定义按键btn2->resize(110,40);                                //重置按键大小btn2->move(btn1->x()+btn1->width()+80,btn1->y());      //移动按键位置
}Widget::~Widget()
{
}

结果: 

 

3> 思维导图

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com