QT day2

widget.cpp

#include "widget.h"
#include "ui_widget.h"
 
  
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    qDebug()<< this->size();    //获取界面的尺寸
    this->setFixedSize(380,600);  //固定界面尺寸
    this->setWindowTitle("小黑子app");     //设置窗口标题
    this->setWindowIcon(QIcon("C:\\Users\\Administrator\\Desktop\\QQ20230915190020.png"));
    QPushButton *btn1 = new QPushButton(this);  //构造一个按钮并直接指定父组件
    btn1->setText("登录");    //设置按钮文本内容
    btn1->move(80,500);
    btn1->resize(100, 40);
    //构建按钮2
    QPushButton *btn2 = new QPushButton(this);
    btn2->setText("取消");
    btn2->move(btn1->x()+150, btn1->y());
    btn2->resize(100, 40);
    //构建一个行编辑器并指定父组件
    QLineEdit *edit1 = new QLineEdit(this);
    edit1->resize(250, 50);
    edit1->move(120, 350);
    QLineEdit *edit2 = new QLineEdit(this);
    edit2->resize(250, 50);
    edit2->move(edit1->x(),edit1->y()+70);
    edit1->setPlaceholderText("树脂666");
    edit2->setEchoMode(QLineEdit::Password);    //设置回显
    //标签
    QLabel *lab1 = new QLabel(this);
    lab1->resize(50,50);
    lab1->setStyleSheet("background-color:yellow;");
    lab1->move(40, 350);
    lab1->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\QQ20230915211012.png"));
    lab1->setScaledContents(true);
    QLabel *lab2 = new QLabel(this);
    lab2->resize(50, 50);
    lab2->setStyleSheet("background-color:yellow;");
    lab2->move(lab1->x(), lab1->y()+70);
    lab2->setPixmap(QPixmap("C:\\Users\\Administrator\\Desktop\\QQ20230915211447.png"));
    lab2->setScaledContents(true);
    QMovie *movie = new QMovie("C:\\Users\\Administrator\\Desktop\\QQ20230915200707.gif");
    movie->start();
    QLabel *lab3 = new QLabel(this);
    lab3->setGeometry(0,0,600,340);
    lab3->setMovie(movie);
     lab3->setStyleSheet("background-color:yellow;");
 
  
        //QT4版手动连接buton1信号和槽
        connect(this->btn1,SIGNAL(clicked()),this,SLOT(but1_slot()));
        //QT5版手动连接button2信号和槽
      connect(this->btn2, &QPushButton::clicked, this, &Widget::btn2_slot);
 
  
 
  
}
 
  
Widget::~Widget()
{
    delete ui;
}
 
  
 
  
void Widget::btn1_slot()
{
    QMessageBox msgbox;     //实例化对象
    if(edit1->text()=="admin" &&edit2->text()=="123456")
    {
        msgbox.setIcon(QMessageBox::NoIcon);
                //msgbox.setWindowTitle("QQ");
                msgbox.setText("登陆成功");
                msgbox.setStandardButtons(QMessageBox::Ok);
                msgbox.setDefaultButton(QMessageBox::Ok);
                msgbox.exec();
                emit btn1_signal();
                this->close();
    }
    else
    {
        QMessageBox msgBox;
          int ret;
 
  
          msgbox.setIcon(QMessageBox::Critical);
          msgbox.setWindowTitle("QQ");
          msgbox.setText("账号密码不匹配,是否重新登录?");
          msgbox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
          msgbox.setDefaultButton(QMessageBox::Ok);
          ret = msgBox.exec();
          if(ret == QMessageBox::Ok)
          {
                edit2->clear();
          }
           else if(ret == QMessageBox::Cancel)
          {
                msgbox.close();
          }
    }
 }
 
  
void Widget::btn2_slot()
{
    QMessageBox::StandardButton ret = QMessageBox::warning(             //调用静态成员函数,需要加上类名和作用域限定符
                                                   this,                 //父组件
                                                   //"QQ",                  //对话框标题
                                                   "是否确定退出登录?",        //对话框文本内容
                                                   QMessageBox::Yes|QMessageBox::No,   //提供的按钮
                                                   QMessageBox::Yes );                  //默认按钮
 
  
 
  
      //对用户点击的结果进行判断
      if(ret == QMessageBox::No)
      {
          this->close();
      }
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
 
  
#include 
#include 
#include 
#include 
#include 
#include
#include
#include "second.h"
 
  
 
  
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
 
  
class Widget : public QWidget
{
    Q_OBJECT
 
  
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
 
  
signals:
    void btn1_signal();
 
  
public slots:
    void btn1_slot();
    void btn2_slot();
 
  
private:
    Ui::Widget *ui;
    QPushButton *btn1;
    QPushButton *btn2;
    QLineEdit *edit1;
    QLineEdit *edit2;
    QLabel *lab1;
    QLabel *lab2;
    QLabel *lab3;
 
  
 
  
};
#endif // WIDGET_H

main.c

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

second.cpp

#include "second.h"
#include "ui_second.h"
 
  
second::second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::second)
{
    ui->setupUi(this);
    //窗口属性
    this->setFixedSize(480,360);
    this->setWindowTitle("网络聊天室");
 
  
}
 
  
second::~second()
{
    delete ui;
}
 
  
 
  
void second::btn1_jumpslot()
{
    this->show();
second.h}
#ifndef SECOND_H
#define SECOND_H
 
  
#include 
 
  
namespace Ui {
class second;
}
 
  
class second : public QWidget
{
    Q_OBJECT
 
  
public:
    explicit second(QWidget *parent = nullptr);
    ~second();
 
  
private:
    Ui::second *ui;
 
  
public slots:
    void btn1_jumpslot();
 
  
};
 
  
#endif // SECOND_H
 
  
 
  
 
 

你可能感兴趣的:(qt,开发语言)