• 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);
    }

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

  • 相关阅读:
    ZJCTF预赛一个.py的逆向题
    if(a)是什么意思
    整理OD一些快捷键和零碎知识点
    NSCTF-Reverse02 超级详细且简单的办法搞定
    CTF实验吧——证明自己吧
    Beat our dice game and get the flag 击败我们的骰子游戏拿到旗子
    CTF-Keylead(ASIS CTF 2015)
    【第三届强网杯】两道杂项题的wp
    【实验吧】该题不简单——writeup
    嵩天老师python网课爬虫实例1的问题和解决方法
  • 原文地址:https://www.cnblogs.com/newstart/p/2838879.html
Copyright © 2020-2023  润新知