Qt 主界面菜单栏和状态栏实现

简介:

因为之前一直用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);
}
复制代码

不做解释,代码基本都有注释!


本文转自夜&枫博客园博客,原文链接:http://www.cnblogs.com/newstart/archive/2012/12/29/2838879.html,如需转载请自行联系原作者

相关文章
|
8月前
Qt6学习笔记三(QMainWindow、菜单栏、工具栏、状态栏、铆接部件、核心部件)
Qt6学习笔记三(QMainWindow、菜单栏、工具栏、状态栏、铆接部件、核心部件)
221 0
|
5月前
|
API UED
【Qt 学习笔记】Qt窗口 | 状态栏 | QStatusBar的使用及说明
【Qt 学习笔记】Qt窗口 | 状态栏 | QStatusBar的使用及说明
706 4
|
5月前
|
数据可视化
【Qt 学习笔记】Qt窗口 | 菜单栏 | QMenuBar的使用及说明
【Qt 学习笔记】Qt窗口 | 菜单栏 | QMenuBar的使用及说明
1230 2
|
8月前
|
Windows
[Qt5] 创建菜单栏、工具栏、核心控件和浮动窗口
[Qt5] 创建菜单栏、工具栏、核心控件和浮动窗口
159 0
《QT从基础到进阶·九》菜单,菜单栏,工具栏创建
《QT从基础到进阶·九》菜单,菜单栏,工具栏创建
79 0
QT桌面项目(状态栏和导航栏设置)
QT桌面项目(状态栏和导航栏设置)
286 0
|
API
利用QT实现主界面APP应用开发之经典
利用QT实现主界面APP应用开发之经典
320 1
利用QT实现主界面APP应用开发之经典
|
数据可视化
Qt 状态栏QStatusBar
Qt 状态栏QStatusBar
504 0
Qt 状态栏QStatusBar
|
API Python
Python Qt GUI设计:菜单栏、工具栏和状态栏的使用方法(拓展篇—2)
Python Qt GUI设计:菜单栏、工具栏和状态栏的使用方法(拓展篇—2)
Python Qt GUI设计:菜单栏、工具栏和状态栏的使用方法(拓展篇—2)

推荐镜像

更多