用qt写一个闹钟的UI界面
时间: 2023-05-22 09:02:32 浏览: 225
好的,以下是一个用Qt编写的简单闹钟UI界面的代码示例。
```cpp
#include <QApplication>
#include <QBoxLayout>
#include <QComboBox>
#include <QDateTime>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QSpinBox>
#include <QTime>
#include <QWidget>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QWidget *widget = new QWidget();
widget->resize(400, 200);
QLabel *title = new QLabel("闹钟");
title->setFont(QFont("Arial", 20, QFont::Bold));
QHBoxLayout *topLayout = new QHBoxLayout();
topLayout->addWidget(title, 0, Qt::AlignHCenter);
QLabel *hourLabel = new QLabel("小时:");
QSpinBox *hourSpinBox = new QSpinBox();
hourSpinBox->setRange(0, 23);
hourSpinBox->setValue(QTime::currentTime().hour());
QHBoxLayout *hourLayout = new QHBoxLayout();
hourLayout->addWidget(hourLabel);
hourLayout->addWidget(hourSpinBox);
QLabel *minuteLabel = new QLabel("分钟:");
QSpinBox *minuteSpinBox = new QSpinBox();
minuteSpinBox->setRange(0, 59);
minuteSpinBox->setValue(QTime::currentTime().minute());
QHBoxLayout *minuteLayout = new QHBoxLayout();
minuteLayout->addWidget(minuteLabel);
minuteLayout->addWidget(minuteSpinBox);
QLabel *soundLabel = new QLabel("响铃:");
QComboBox *soundComboBox = new QComboBox();
soundComboBox->addItem("无声");
soundComboBox->addItem("铃声1");
soundComboBox->addItem("铃声2");
soundComboBox->addItem("铃声3");
QHBoxLayout *soundLayout = new QHBoxLayout();
soundLayout->addWidget(soundLabel);
soundLayout->addWidget(soundComboBox);
QPushButton *confirmButton = new QPushButton("确定");
QPushButton *cancelButton = new QPushButton("取消");
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(confirmButton);
buttonLayout->addWidget(cancelButton);
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addLayout(topLayout);
mainLayout->addLayout(hourLayout);
mainLayout->addLayout(minuteLayout);
mainLayout->addLayout(soundLayout);
mainLayout->addLayout(buttonLayout);
widget->setLayout(mainLayout);
widget->show();
return a.exec();
}
```
希望这个代码示例能为你提供帮助!
阅读全文