因为之前一直用c#来着,最近项目需要跨平台
所以研究Qt发现上手也很快
学习QT学习到后面越发现Qt有些功能很强大
这里展示一个小demo,适合初学者高手绕行。。。
登陆界面
主界面:
代码部分:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "QLabel.h"
namespace Ui {
class MainWindow;
}
class Action;
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
private slots:
void on_action_2_activated();
private:
QAction *openAction;
QLabel *msgLabel;
QLabel *ztgLabel;
QLabel *zsgLabel;
private slots:
void timerUpDate();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDesktopWidget.h"
#include "QTextCodec.h"
#include "QMessageBox.h"
#include "frmdlg.h"
#include "QDateTime.h"
#include "QTimer.h"
#include "QProgressBar.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->resize(800,500);
//居中设置
QDesktopWidget* desktop = QApplication::desktop();
int width = desktop->width();
int height = desktop->height();
move((width - this->width())/2, (height - this->height())/2);
QTimer *timer = new QTimer(this);
//新建定时器
connect(timer,SIGNAL(timeout()),this,SLOT(timerUpDate()));
//关联定时器计满信号和相应的槽函数
timer->start(1000);
//状态栏初始化
QTextCodec::setCodecForTr( QTextCodec::codecForName("GBK") );
msgLabel=new QLabel();
this->ui->statusBar->addPermanentWidget(msgLabel);
ztgLabel=new QLabel();
this->ui->statusBar->addWidget(ztgLabel);
QProgressBar *progressBar = new QProgressBar();
progressBar->setTextVisible( false );
progressBar->setRange(0,0);
this->ui->statusBar->addWidget(progressBar,1);
// QStatusBar的子组件的border设置为0,也就是没有边框
// statusBar()->setStyleSheet(QString("QStatusBar::item{border: 0px}"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_action_2_activated()
{
QTextCodec::setCodecForTr( QTextCodec::codecForName("GBK") );
//MessageBox提示框
//QMessageBox::warning(this,tr("警告"),tr("用户名或密码错误!"),QMessageBox::Yes);
//打开子窗体
FrmDlg *dlg=new FrmDlg();
dlg->show();
}
void MainWindow::timerUpDate()
{
QTextCodec::setCodecForTr( QTextCodec::codecForName("GBK") );
QDateTime time = QDateTime::currentDateTime();
//获取系统现在的时间
QString str = time.toString("yyyy-MM-dd hh:mm:ss ddd"); //设置显示格式
//设置系统时间显示格式
ui->label->setText(str);
//在标签上显示时间
ui->label_2->setText(tr("每秒产生一个随机数:%1").arg(qrand()%10));
ztgLabel->setText(tr("通信状态:%1").arg(qrand()%10));
msgLabel->setText(str);
}