当前位置: 首页 > article >正文

qt小项目:表白窗口(窗口,信号与槽)

在小的时候,我们经常会看到程序员的表白链接或者表白软件,今天我们来用qt做一个简易版本,并复习一下窗口,信号与槽的操作

1.整体思路

在窗口中显示一个按钮,写着表白话语,然后两个按钮,yes和no,点no则该按钮就跑,点yes则出现约会话语,再点就关闭窗口

2.分布实现思路

表白按钮直接写,yes按钮要一个点击信号,槽的功能是出现约会话语按钮,no按钮则是根据随机数来改变按钮的位置

3.整体代码

可以给女朋友下一个qt creater然后复制过去,我的天好浪漫

pro

QT       += core gui
QMAKE_PROJECT_DEPTH = 0

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mainwindow.h 

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QPushButton>
#include<ctime>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public slots:
    void changeq1();
    void changeq2();


private:
    Ui::MainWindow *ui;
public:
    QPushButton *q1;
    QPushButton*q2;

};
#endif // MAINWINDOW_H

mainwindow.cpp

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    q1=new QPushButton("yes",this);
    q1->setGeometry(300,200,50,50);
   q2=new QPushButton("no",this);
    q2->setGeometry(450,200,50,50);
   std::srand(std::time(nullptr));
    connect(q1, &QPushButton::clicked, this, &MainWindow::changeq1);
    connect(q2, &QPushButton::clicked, this, &MainWindow::changeq2);
}

void MainWindow::changeq1()
{

   QPushButton* q3=new QPushButton("have a dinner?",this);
    q3->setGeometry(250,500,300,100);
   q3->show();
    connect(q3, &QPushButton::clicked, this, &MainWindow::close);
}
void MainWindow:: changeq2()
{
    int x=std::rand()%100;
    int y=std::rand()%100;
    q2->setGeometry(x,y,50,50);
}
MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp

#include "mainwindow.h"

#include <QApplication>
#include<QObject>
#include<QPushButton>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    QPushButton *q=new QPushButton("do you love me?",&w);
    q->setGeometry(300,300,200,100);
    w.show();
    return a.exec();
}


http://www.kler.cn/a/550679.html

相关文章:

  • UI自动化教程 —— 元素定位技巧:精确找到你需要的页面元素
  • Node.js 中的 fs 模块详解
  • AI时代:前端开发的职业发展路径拓宽
  • 洛谷P8707 [蓝桥杯 2020 省 AB1] 走方格
  • Qt 中使用 SQLite 数据库的完整指南
  • 【MyBatis】预编译SQL与即时SQL
  • 『大模型笔记』Jason Wei: 大语言模型的扩展范式!
  • 使用Navicat for MySQL工具连接本地虚拟机上的MySQL
  • Ubantu安装Prometheus、Grafana、node_exporter、elasticsearch_exporter监控运行状态
  • linux中(base) [root@ZS ~]# 是什么情况,怎么退出
  • 【C++指南】类和对象(十):const成员函数
  • ctf网络安全大赛python
  • Mysql-死锁图文详解
  • 前端JS接口加密攻防实操
  • 进阶——第十六届蓝桥杯嵌入式熟练度练习(开发板捕获频率和占空比)
  • OpenCV中的边缘检测
  • 基于YOLO11深度学习的胃肠道息肉智能检测分割与诊断系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标分割、人工智能
  • uniapp 安卓10+ 选择并上传文件
  • 数据驱动业务增长,E-MapReduce 真实案例解析
  • 数据分析简介及其常用指标与方法