日历组件
头文件#include <QCalendarWidget>
mainwindow.h
#ifndef LEFT_MAINWINDOW_H
#define LEFT_MAINWINDOW_H
#include <QWidget>
#include <QCalendarWidget>
#include <QPushButton>
#include <QComboBox>
#include "add_todo.h"
class add_todo;
class left_mainWindow : public QWidget
{
Q_OBJECT
public:
left_mainWindow(QWidget *parent = nullptr);
~left_mainWindow();
void adjustpos(const QRect&);
private:
QCalendarWidget *calendar;
QPushButton *add_new;
QPushButton *fold_button,*setting_button;
QComboBox *workcomboBox;
add_todo *add_window;
bool folded;
signals:
private slots:
void foldwindow();
void add_files();
};
#endif // LEFT_MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例 QLabel 控件
label = new QLabel(this);
label->setText("选择日期:");
//位置
label->setGeometry(QRect(50,50,100,25));
lineEdit = new QLineEdit(this);
lineEdit->setGeometry(QRect(110,50,150,22));
//事件
connect(lineEdit,SIGNAL(cursorPositionChanged(int,int)),this,SLOT(showTime()));
//实例时间控件
calendarWidget = new QCalendarWidget(this);
//位置
calendarWidget->setGeometry(20,75,350,180);
//隐藏时间控件
calendarWidget->setHidden(true);
//时间控件点击事件
connect(calendarWidget,SIGNAL(clicked(QDate)),this,SLOT(setData()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showTime()
{
calendarWidget->setHidden(false);
}
void MainWindow::setData()
{
//接收选择时间
QDate date = calendarWidget->selectedDate();
//时间格式化
QString str = date.toString("yyyy-MM-dd");
//赋值
lineEdit->setText(str);
//日期控件隐藏
calendarWidget->setHidden(true);
}
要注意如何获取日历中的数据。
登录窗口
暂时无
文件浏览对话框
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit> //单行文本控件类
#include <QPushButton> //按钮类
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private slots:
void showFiles();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QLineEdit *filename;
QPushButton *button;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例单行文本控件
filename = new QLineEdit(this);
//位置
filename->setGeometry(QRect(50,50,230,25));
//实例按钮
button = new QPushButton(this);
//按钮位置
button->setGeometry(QRect(280,50,80,25));
//按钮名
button->setText("浏览");
//按钮点击事件
connect(button,SIGNAL(clicked()),this,SLOT(showFiles()));
}
MainWindow::~MainWindow()
{
delete ui;
}
//按钮点击方法
void MainWindow::showFiles()
{
//定义变量 str 接收 QFileDialog 对话框获取的文件路径
QString str = QFileDialog::getOpenFileName(this,"open file","/","textfile(*.txt);;C file(*.cpp);;All file(*.*)");
//将变量绑定 QLineEdit 控件
filename->setText(str.toUtf8());
}
颜色选择对话框
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private slots:
void editText();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QPushButton *button;
QLabel *label;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QColorDialog> //引用 QColorDialog 类
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例按钮
button = new QPushButton(this);
//位置
button->setGeometry(QRect(200,50,80,25));
//按钮值
button->setText("选择颜色");
//按钮事件
connect(button,SIGNAL(clicked()),this,SLOT(editText()));
//实例 QLabel
label = new QLabel(this);
//label 位置
label->setGeometry(QRect(50,50,100,25));
//label 值
label->setText("我的颜色可以改变");
}
MainWindow::~MainWindow()
{
delete ui;
}
//修改方法
void MainWindow::editText()
{
//颜色对话框设置
QColorDialog::setCustomColor(0,QRgb(0x0000FF));
//定义 QColor 接收值
QColor color = QColorDialog::getColor(QColor(0,255,0));
//定义 QPalette(调色板类)
QPalette p = palette();
//调色板接收颜色
p.setColor(QPalette::WindowText,QColor(color));
//给 label 绑定颜色
label->setPalette(p);
}
进度条实例
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton> //按钮类
#include <QProgressBar> //进度条类
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private slots:
void startBar();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QPushButton *button;
QProgressBar *bar;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QColorDialog> //引用 QColorDialog 类
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例 QProgressBar 控件
bar = new QProgressBar(this);
//位置
bar->setGeometry(QRect(50,50,200,20));
//范围值
bar->setRange(0,100000-1);
//初始值
bar->setValue(0);
//实例 Button
button = new QPushButton(this);
//位置
button->setGeometry(QRect(270,50,80,25));
//值
button->setText("开始");
//事件
connect(button,SIGNAL(clicked()),this,SLOT(startBar()));
}
MainWindow::~MainWindow()
{
delete ui;
}
//修改方法
void MainWindow::startBar()
{
for(int i=0;i<100000;i++)
{
//100%结束
if(i == 99999)
{
button->setText("结束");
}
//赋值
bar->setValue(i);
}
}
Timer实时更新时间
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private slots:
void timerTime();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QLabel *label;
QTimer *timer;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("test app");
this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
// this->setWindowFlags(Qt::FramelessWindowHint);
// this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setStyleSheet("background:red");
//实例 QLabel 控件
label = new QLabel(this);
//位置
label->setGeometry(QRect(50,50,200,25));
//实例 QTimer 控件
timer = new QTimer;
//timer 时间
connect(timer,SIGNAL(timeout()),this,SLOT(timerTime()));
//执行
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerTime()
{
//获取系统时间
QDateTime sysTime = QDateTime::currentDateTime();
label->setText(sysTime.toString());
}