QT新手日记025 - W002程序代码

一、pro文件:W002.pro

cpp 复制代码
QT       += core gui

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 \
    wintwo.cpp

HEADERS += \
    wintwo.h

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

RESOURCES +=

二、头文件:wintwo.h

cpp 复制代码
#ifndef WINTWO_H
#define WINTWO_H

#include <QMainWindow>

class WinTwo : public QMainWindow
{
    Q_OBJECT

public:
    WinTwo(QWidget *parent = nullptr);
    ~WinTwo();
    QString  GetTipInformation(int Index,QDate d,QTime t,QString obj);
private:
    int nowIndex=0;
};
#endif // WINTWO_H

三、主程序:main.cpp

cpp 复制代码
#include "wintwo.h"

#include <QApplication>

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

四、核心程序:wintwo.cpp

cpp 复制代码
#include "wintwo.h"
#include<QGroupBox>
#include<QLabel>
#include<QComboBox>
#include<QDateEdit>
#include<QStringList>
#include<QTextEdit>
#include<QPushButton>
#include<QFile>
#include<QDir>
#include<QDebug>
#include<QGuiApplication>
#include<QScreen>


WinTwo::WinTwo(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("QT第二个程序 - 每日一题");
    setFixedSize(304,504);
    QGroupBox *grp = new QGroupBox("每日一题",this);
    grp->move(2,2);
    grp->resize(300,500);

    QLabel *l1 = new QLabel("日期:",grp);
    l1->resize(80,30);
    l1->move(10,30);

    QLabel *l2 = new QLabel("时间:",grp);
    l2->resize(80,30);
    l2->move(10,60);

    QLabel *l3 = new QLabel("主题:",grp);
    l3->resize(80,30);
    l3->move(10,90);

    QDateEdit *cmbdate = new QDateEdit(grp);
    cmbdate->resize(120,30);
    cmbdate->move(95,30);
    cmbdate->setDate(QDate::currentDate());
    QTimeEdit *cmbtime = new QTimeEdit(grp);
    cmbtime->resize(120,30);
    cmbtime->move(95,60);
    cmbtime->setTime(QTime::currentTime());
    QComboBox *cmbobj = new QComboBox(grp);
    cmbobj->resize(120,30);
    cmbobj->move(95,90);

    cmbobj->addItems({"QWidget","QMainWindow","QDialog"});
    cmbobj->setCurrentIndex(0);

    QTextEdit *txt = new QTextEdit(grp);
    txt->resize(280,500-140);
    txt->move(10,130);
    txt->setReadOnly(true);
    QPushButton *btnup = new QPushButton("上一题",grp);
    btnup->resize(65,40);
    btnup->move(225,30);
    QPushButton *btndw = new QPushButton("下一题",grp);
    btndw->resize(65,40);
    btndw->move(225,80);
    connect(btnup,&QPushButton::clicked,this,[=](){
        if(nowIndex<=0)
        {
            nowIndex=100;
        }
        else
        {
            nowIndex--;
        }
          txt->setText(GetTipInformation(nowIndex,cmbdate->date(),cmbtime->time(),cmbobj->currentText()));

        //上一题
    });
    connect(btndw,&QPushButton::clicked,this,[=](){
        if(nowIndex>=100)
        {
            nowIndex=0;
        }
        else
        {
            nowIndex++;
        }
          txt->setText(GetTipInformation(nowIndex,cmbdate->date(),cmbtime->time(),cmbobj->currentText()));
        //上一题
    });
    txt->setText(GetTipInformation(nowIndex,cmbdate->date(),cmbtime->time(),cmbobj->currentText()));

    this->move((QGuiApplication::primaryScreen()->size().width()-this->width())/2,
               (QGuiApplication::primaryScreen()->size().height()-this->height())/2);
}

QString  WinTwo::GetTipInformation(int Index,QDate d,QTime t,QString obj)
{
    QString path=QDir::currentPath()+ "/TipFiles/Tipinfo"+QString::number(Index)+".txt";
    QFile file(path);
    QString dat="没有发现文件"+path;
    if(file.exists())
    {
        file.open(QIODevice::ReadOnly);
        dat=file.readAll();
        file.close();
    }
    QString result=QString("每日一题[%1 %2]=>%3:%4\n%5").
            arg(d.toString("yyyy-MM-dd")).arg(t.toString("hh:mm:ss")).arg(obj).
            arg(QString::number(Index)).arg(dat);

    return result;
}
WinTwo::~WinTwo()
{
}

五、每日一题内容以Tipinfo0..n.txt命名的文件放在编译目录下的TipFiles目录下。

完成的界面截图(Deepin系统)

相关推荐
Wang's Blog4 小时前
Java 项目实战: 交友项目:登录链路回顾与今日任务地图
java·开发语言·状态模式·交友
砚底藏山河4 小时前
python量化:行情数据回溯
java·开发语言·python·金融·maven
今年下半年4 小时前
AI智能体项目实战记录(02):初始化 Python 项目
开发语言·人工智能·python·fastapi
Williamtym4 小时前
PickGo Lab实践:用浏览器、Python和MCP串起机器人搬运仿真
开发语言·python·机器人
DongQiShanRen5 小时前
六根硬件映射(四):归属澄清、两处不一致与卷十一总表
开发语言·人工智能·算法·rust·哈希算法·集成学习
CS创新实验室5 小时前
Python 对象“死“了之后去了哪?——引用计数与分代垃圾回收
开发语言·jvm·python
quantdash_cc5 小时前
Pandas 处理分钟级股票行情:从原始数据清洗到 K 线聚合
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
yu_zhidun5 小时前
企业电脑监控软件怎么选:别只看功能清单,先看这 6 个底层能力
开发语言·电脑·php·域智盾系统
把我格子衫拿来5 小时前
VTK 9.5 与 Qt 6 联合开发:从嵌入三维窗口到可维护的桌面架构
c++·python·qt·vtk·三维可视化
无忧.芙桃6 小时前
C++语言原理与实践(十六):多态机制讲解
开发语言·c++·多态