23062QTday1

自己制作一个登录界面

23062QTday1_第1张图片

头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

源文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    //设置固定尺寸
    this->setFixedSize(640,480);
    //给程序框重命名并添加图片
    this->setWindowTitle("Widget");
    this->setWindowIcon(QIcon("C:\\Users\\LENOVO\\Desktop\\icon\\wodepeizhenshi.png"));

    //设置登录按钮
   QPushButton *btn1=new QPushButton(QIcon("C:\\Users\\LENOVO\\Desktop\\icon\\login.png"),"登录",this);
   btn1->setParent(this);
   btn1->resize(100,50);
   btn1->move(220,400);

    //设置取消按钮
   QPushButton *btn2=new QPushButton(QIcon("C:\\Users\\LENOVO\\Desktop\\icon\\cancel.png"),"取消",this);
   btn2->setParent(this);
   btn2->resize(btn1->size());
   btn2->move(btn1->x()+120,btn1->y());

   //添加账号图片
   QLabel *lab1=new QLabel(this);
   lab1->setPixmap(QPixmap("C:\\Users\\LENOVO\\Desktop\\icon\\userName.jpg"));
   lab1->setScaledContents(true);
   lab1->resize(50,50);
   lab1->move(80,250);
   //添加密码图片
   QLabel *lab2=new QLabel(this);
   lab2->setPixmap(QPixmap("C:\\Users\\LENOVO\\Desktop\\icon\\passwd.jpg"));
   lab2->setScaledContents(true);
   lab2->resize(50,50);
   lab2->move(lab1->x(),lab1->y()+60);
   //添加账号文本框
   QLineEdit *edit1=new QLineEdit(this);
   edit1->resize(400,50);
   edit1->move(lab1->x()+60,lab1->y());
   edit1->setMaxLength(11);
   edit1->setPlaceholderText("账号/手机/邮箱");
   //添加密码文本框
   QLineEdit *edit2=new QLineEdit(this);
   edit2->resize(400,50);
   edit2->move(lab2->x()+60,lab2->y());
   edit2->setEchoMode(QLineEdit::Password);
   edit2->setMaxLength(11);

    //添加顶层图片
   QLabel* lab3=new QLabel(this);
   lab3->setPixmap(QPixmap("C:\\Users\\LENOVO\\Desktop\\icon\\logo.png"));
   lab3->setScaledContents(true);
   lab3->resize(640,220);


}

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

测试文件:

#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

运行效果:

23062QTday1_第2张图片

思维导图

23062QTday1_第3张图片23062QTday1_第4张图片 

你可能感兴趣的:(c++)