QT5:如何将窗口大小从最小化更改为正常?

时间:2017-06-25 17:15:44

标签: c++ qt qt5 c++14 qt5.5

我的程序在启动时自动运行。我的程序开始最小化。

示例:

My program run at startup minimized

我使用此命令执行此操作:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    QRect r1(QApplication::desktop()->screenGeometry(this).center().x()-300,
             QApplication::desktop()->screenGeometry(this).center().y()-300, 100, 200);

    MainWindow::setGeometry(r1);

    srand(std::time(0));

    ui->setupUi(this);

    MainWindow::showMinimized();

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(MyTimerSlots()) );
    timer->start(5000);
}

但如果它现在是晚上八点(20:00),我希望看到该程序的正常大小。我希望看到类似的东西:

enter image description here

我尝试寻找解决方案: 我用这个命令MainWindow :: showNormal();但没有成功

void MainWindow::MyTimerSlots()
{
qDebug() << "Timer..."<<QTime::currentTime().hour();

if (QTime::currentTime().hour() == 20 ){
    //MainWindow::showFullScreen();
    MainWindow::showNormal();

}

}

如何在晚上20点将窗口大小从最小化更改为正常?

更新

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QWidget>

#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QTimer *timer;

public slots:
    void MyTimerSlots();


private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


#include <QDebug>

#include <QStyle>
#include <QDesktopWidget>

#include <QTime>
#include <QDate>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QRect r1(QApplication::desktop()->screenGeometry(this).center().x()-300,
           QApplication::desktop()->screenGeometry(this).center().y()-300, 100, 200);

    setGeometry(r1);
    srand(std::time(0));
    showMinimized();

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(MyTimerSlots()) );
    timer->start(5000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

 void MainWindow::MyTimerSlots()
{
    qDebug() << "Timer..."<<QTime::currentTime().minute();

    //MainWindow::showFullScreen();

    showNormal();
    MainWindow::activateWindow();
    MainWindow::raise();

    //MainWindow::activateWindow();

}

0 个答案:

没有答案
相关问题