Qt 主界面菜單欄和狀態欄實現


因為之前一直用c#來着,最近項目需要跨平台

所以研究Qt發現上手也很快

學習QT學習到后面越發現Qt有些功能很強大

這里展示一個小demo,適合初學者高手繞行。。。

登陸界面

主界面:

代碼部分:

View Code
#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
View Code
#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);
}

不做解釋,代碼基本都有注釋!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM