QT-day1作业

cpp 复制代码
#include "mywnd.h"
#include <iostream>
#include <QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>

MyWnd::MyWnd(QWidget *parent)
    : QWidget(parent)
{
    qDebug()<<this->size();          //获取当前界面的尺寸
    qDebug()<<this->width();          //获取当前组件的宽度
    qDebug()<<this->height();          //获取当前组件的高度
    this->setFixedSize(500,400);       //设置固定尺寸
    qDebug()<<this->windowTitle();        //获取当前组件的窗口标题
    this->setWindowTitle("Widget");        //设置窗口标题
    this->setWindowIcon(QIcon("C:\\Users\\m\\Desktop\\icon\\01.png"));         //设置窗口图标
    this->setStyleSheet("background-color:white;");                              //设置样式表
    this->setWindowOpacity(1);             //设置窗口

    //使用无参构造函数,构造一个按钮
    QPushButton *btn1=new QPushButton;          //无参构造
    btn1->setParent(this);                //将当前界面作为父组件
    btn1->setText("登录");              //设置按钮上的文本内容
    qDebug()<<btn1->size();              //获取按钮的尺寸
    btn1->resize(80,40);               //重新设置组件的尺寸
    btn1->setIcon(QIcon("C:\\Users\\m\\Desktop\\icon\\login.png"));            //设置图标
    btn1->move(150,320);             //移动组件位置
    btn1->setStyleSheet("background-color:gray;");                         //设置样式表

    //构造一个按钮,构造时直接指定父组件
    QPushButton *btn2=new QPushButton(this);
    btn2->setText("取消");
    btn2->resize(btn1->size());                 //使用其他组件的尺寸给该组件赋值
    btn2->move(btn1->x()+150,btn1->y());          //使用其他组件的位置给该组件定位
    btn2->setIcon(QIcon("C:\\Users\\m\\Desktop\\icon\\cancel.png"));            //设置图标
    btn2->setStyleSheet("background-color:gray;");                         //设置样式表

    //实例化一个标签,并指定父组件
    QLabel *lab1=new QLabel(this);
    lab1->resize(500,150);                      //重新设置尺寸
    lab1->setStyleSheet("background-color:gray;");                         //设置样式表
    qDebug()<<lab1->text();                //获取标签文本内容
    //lab1->setAlignment(Qt::AlignCenter);       //文本居中对齐
    lab1->setPixmap(QPixmap("C:\\Users\\m\\Desktop\\icon\\logo.png"));        //设置图片
    lab1->setScaledContents(true);                       //设置内容自适应

    //再实例化一个标签,并指定父组件
    QLabel *lab2=new QLabel(this);
    lab2->resize(50,40);                      //重新设置尺寸
    lab2->setStyleSheet("background-color:gray;");                         //设置样式表
    lab2->setPixmap(QPixmap("C:\\Users\\m\\Desktop\\icon\\userName.jpg"));        //设置图片
    lab2->setScaledContents(true);                       //设置内容自适应
    lab2->move(100,180);             //移动组件位置

    //再实例化一个标签,并指定父组件
    QLabel *lab3=new QLabel(this);
    lab3->resize(lab2->size());                 //使用其他组件的尺寸给该组件赋值
    lab3->move(lab2->x(),lab2->y()+60);          //使用其他组件的位置给该组件定位
    lab3->setStyleSheet("background-color:gray;");                         //设置样式表
    lab3->setPixmap(QPixmap("C:\\Users\\m\\Desktop\\icon\\passwd.jpg"));        //设置图片
    lab3->setScaledContents(true);                       //设置内容自适应

    //构造一个行编辑器对象,并指定父组件
    QLineEdit *edit1=new QLineEdit(this);
    edit1->resize(250,40);                     //重新设置尺寸
    edit1->move(lab2->x()+60,lab2->y());         //移动组件位置
    //edit1->setEchoMode(QLineEdit::Password);       //设置回显模式
    edit1->setMaxLength(6);              //设置文本最大长度
    edit1->setPlaceholderText("用户名");     //设置占位文本
    edit1->setAlignment(Qt::AlignCenter);       //文本居中对齐

    //再构造一个行编辑器对象,并指定父组件
    QLineEdit *edit2=new QLineEdit(this);
    edit2->resize(edit1->size());                   //使用其他组件的尺寸给该组件赋值
    edit2->move(lab3->x()+60,lab3->y());         //移动组件位置
    edit2->setEchoMode(QLineEdit::Password);       //设置回显模式
    edit2->setMaxLength(6);              //设置文本最大长度
    edit2->setPlaceholderText("用户密码");     //设置占位文本
    edit2->setAlignment(Qt::AlignCenter);       //文本居中对齐

}

MyWnd::~MyWnd()
{
}

结果:

相关推荐
larance3 分钟前
python中的鸭子类型
开发语言·python
丙寅33 分钟前
微信小程序反编译遇到 TypeError: _typeof3 is not a function
开发语言·javascript·ecmascript
醇氧34 分钟前
MAC 安装openJDK8
java·开发语言
海阔天空在前走37 分钟前
JAVA中六种策略模式的实现
java·开发语言·策略模式
青衫码上行41 分钟前
【Java Web学习 | 第十篇】JavaScript(4) 对象
java·开发语言·前端·javascript·学习
禁默1 小时前
基于Rust实现爬取 GitHub Trending 热门仓库
开发语言·rust·github
大邳草民1 小时前
深入理解 Python 的属性化方法
开发语言·笔记·python
胎粉仔1 小时前
Swift 初阶 —— Sendable 协议 & data races
开发语言·ios·swift·sendable·并发域·data races
青衫码上行2 小时前
【Java Web学习 | 第九篇】JavaScript(3) 数组+函数
java·开发语言·前端·javascript·学习
jf加菲猫2 小时前
第1章 认识Qt
开发语言·c++·qt·ui