Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

My Window don't show when I start the .exe file



  • Hi,
    So I've made a project, it works when I run it in qtcreator. When I try to start it with the .exe (I added the .dll in the folder) it works well, but nothing shows. I can see it in the task manager, it's not using my processor (it says 0%). I'm on windows 10.
    The code is the following :

    #include <QApplication>
    #include <window.h>
    #include <QDesktopWidget>
    #include <updatemanager.h>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QRect rec = QApplication::desktop()->screenGeometry();
        int height = rec.height();
        int width = rec.width();
        window *win = new window(width,height);
        UpdateManager up(win);
        up.start();
        return app.exec();
    }
    
    #ifndef SCORENUM_H
    #define SCORENUM_H
    
    #include <QLabel>
    #include <QWidget>
    class ScoreNum : public QLabel
    {
        Q_OBJECT
    public:
        ScoreNum(QWidget* parent,int x, int y);
        void SetScore(int sc);
        int GetScore();
    private:
        int score;
    };
    
    #endif // SCORENUM_H
    
    
    #include "scorenum.h"
    
    ScoreNum::ScoreNum(QWidget* parent,int x, int y) : QLabel(parent)
    {
        score = 0;
        setNum(score);
        QFont font("Arial",50);
        setFont(font);
        setGeometry(x,y,100,100);
    }
    void ScoreNum::SetScore(int sc) {
        score = sc;
        setNum(score);
    }
    int ScoreNum::GetScore() {
        return score;
    }
    
    #ifndef UPDATEMANAGER_H
    #define UPDATEMANAGER_H
    #
    #include <atomic>
    #include <chrono>
    #include <iostream>
    #include <thread>
    #include <window.h>
    #include <chrono>
    #include <QObject>
    class UpdateManager : public QObject
    {
        Q_OBJECT
    public :
        UpdateManager(window* window);
        void start();
        void stop();
        void task();
    private :
        bool running;
        std::thread t;
        window* win;
    signals:
        void Updating();
    };
    
    #endif // UPDATEMANAGER_H
    
    #include "updatemanager.h"
    
    UpdateManager::UpdateManager(window* window){
        win = window;
    }
    void UpdateManager::start()
    {
        running = true;
        std::thread t(&UpdateManager::task,this);
        t.detach();
        QObject::connect(this,SIGNAL(Updating()),win,SLOT(updateScreen()));
    }
    void UpdateManager::stop() {
        running = false;
        t.join();
    }
    void UpdateManager::task() {
        while(running) {
            if(win->GetRunning()) {
                win->TranslateBalle();
            }
            win->CheckKey();
            emit Updating();
            std::this_thread::sleep_for(std::chrono::milliseconds(8));
        }
    }
    
    #ifndef WINDOW_H
    #define WINDOW_H
    
    #include <QWidget>
    #include <QPainter>
    #include <QKeyEvent>
    #include <scorenum.h>
    #include <vector>
    class window : public QWidget
    {
        Q_OBJECT
    public:
        window(int width,int height);
        void TranslateBalle();
        void paintEvent(QPaintEvent *event) override;
        void keyPressEvent(QKeyEvent *k) override;
        void keyReleaseEvent(QKeyEvent *k) override;
        void Move();
        void Thread();
        bool CheckIfOut(int posX,int posY, int x, int y, int scaleX, int scaleY);
        bool CheckCollision(int x1, int y1, int x2, int y2, int x3, int y3);
        void Won(std::string side);
        void CheckKey();
        bool GetRunning();
    private:
        double sizeX;
        double sizeY;
        double posYRect1;
        double posYRect2;
        bool running;
        double _posX;
        double _posY;
        double _scaleX;
        double _scaleY;
        double _moveX;
        double _moveY;
        double rectangle1[4];
        double rectangle2[4];
        ScoreNum* score1;
        ScoreNum* score2;
        std::vector<int> keys;
    public slots:
        void updateScreen();
    };
    
    #endif // WINDOW_H
    
    #include "window.h"
    window::window(int width,int height) : QWidget(), sizeX(width), sizeY(height), running(false)
    {
        posYRect1 = 10;
        posYRect2 = 10;
        _scaleX = floor(sizeY/50);
        _scaleY = floor(sizeY/50);
        _posX = floor((sizeX-_scaleX)/2);
        _posY = floor((sizeY-_scaleY)/2);
        _moveX = 10;
        _moveY = 10;
        score1 = new ScoreNum(this,sizeX/2-sizeX/10,sizeY/100);
        score2 = new ScoreNum(this,sizeX/2+sizeX/10,sizeY/100);
        showFullScreen();
    }
    void window::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);
        painter.fillRect(0,0,sizeX,sizeY,Qt::darkGray);
        rectangle1[0] = 10;
        rectangle1[1] = posYRect1;
        rectangle1[2] = sizeX/100;
        rectangle1[3] = sizeY/5;
        painter.fillRect(rectangle1[0],rectangle1[1],rectangle1[2],rectangle1[3],Qt::red);
        rectangle2[0] = sizeX-15;
        rectangle2[1] = posYRect2;
        rectangle2[2] = sizeX/100;
        rectangle2[3] = sizeY/5;
        painter.fillRect(rectangle2[0],rectangle2[1],rectangle2[2],rectangle2[3],Qt::red);
        painter.fillRect(_posX,_posY,_scaleX,_scaleY,Qt::red);
    }
    void window::keyPressEvent(QKeyEvent *k)
    {
        int key = k->key();
        bool isInside = false;
        for(int i = 0 ; i < keys.size();i++) {
            if(key==keys[i]) {
                isInside = true;
            }
        }
        if(!isInside) {
            keys.push_back(key);
        }
    }
    void window::keyReleaseEvent(QKeyEvent *k) {
        int key = k->key();
        for(int i = 0 ; i < keys.size();i++) {
            if(key==keys[i]) {
                keys.erase(keys.begin()+i);
            }
        }
    }
    void window::CheckKey() {
        for(int i = 0 ; i < keys.size() ; i++) {
            switch(keys[i]) {
                case 16777235 : {
                if (posYRect2 != 0) posYRect2 -= 5;
                    break;
                }
                case 16777237 : {
                if(posYRect2 < sizeY-sizeY/5) posYRect2 += 5;
                    break;
                }
                case 83 : {
                if(posYRect1 < sizeY-sizeY/5) posYRect1 += 5;
                    break;
                }
                case 90 : {
                if(posYRect1 !=0) posYRect1 -=5;
                    break;
                }
                case 32 : {
                if(!running)
                {
                    running = true;
                }
            }
            }
        }
    }
    void window::TranslateBalle() {
        if(CheckIfOut(_posX,_posY,_moveX/10,_moveY/10,_scaleX,_scaleY))
        {
            _posX += _moveX/10;
            _posY += _moveY/10;
        }
    }
    bool window::CheckIfOut(int posX1,int posY1,int x1, int y1, int scaleX1, int scaleY1) {
        if(posX1+x1+scaleX1 > sizeX)
        {
            Won("right");
            return false;
        }
        else if(posY1+y1+scaleY1 > sizeY)
        {
            _moveY = -_moveY;
            return true;
        }
        else if(posX1 +x1 < 0)
        {
            Won("left");
            return false;
        }
        else if(posY1 + y1 < 0)
        {
            _moveY = -_moveY;
            return true;
        }
        else
        {
            if(posX1 > sizeX/2)
            {
                int posX2 = rectangle2[0];
                int posY2 = rectangle2[1];
                if(CheckCollision(posX1+scaleX1,posY1,posX2,posY2,posX2+rectangle2[2],posY2+rectangle2[3])) {
                    if(CheckCollision(posX1+scaleX1,posY1+scaleY1,posX2,posY2,posX2+rectangle2[2],posY2+rectangle2[3])) {
                        _moveX = abs(int((2*posY2+rectangle2[3])/2-(2*posY1+scaleY1)/2)) * -0.9;
                        _moveY = -int((2*posY2+rectangle2[3])/2-(2*posY1+scaleY1)/2)*0.9;
                        return true;
                    }
                }
            }
            else
            {
                int posX2 = rectangle1[0];
                int posY2 = rectangle1[1];
                if(CheckCollision(posX1,posY1,posX2,posY2,posX2+rectangle1[2],posY2+rectangle1[3])) {
                    if(CheckCollision(posX1,posY1+scaleY1,posX2,posY2,posX2+rectangle1[2],posY2+rectangle1[3])) {
                        _moveX = abs(int((2*posY2+rectangle1[3])/2-(2*posY1+scaleY1)/2))*0.9;
                        _moveY = -int((2*posY2+rectangle1[3])/2-(2*posY1+scaleY1)/2)*0.9;
                        return true;
                    }
                }
            }
        }
        return true;
    }
    bool window::CheckCollision(int x1, int y1, int x2, int y2, int x3, int y3) {
        if((x2<=x1 && x1<=x3) && (y2<=y1 && y1 <= y3)) {
            return true;
        }
        return false;
    }
    void window::Won(std::string side) {
        _posX = floor((sizeX-_scaleX)/2);
        _posY = floor((sizeY-_scaleY)/2);
        if (side=="left") {
            score2->SetScore(score2->GetScore()+1);
            _moveX = -10;
            _moveY = 0;
        }
        else if(side=="right") {
            score1->SetScore(score1->GetScore()+1);
            _moveX = 10;
            _moveY = 0;
        }
        running = false;
    }
    bool window::GetRunning() {
        return running;
    }
    void window::updateScreen() {
        repaint();
    }
    

  • Moderators

    Open it from command line to see any errors or warnings printed by Qt. You may be missing the platform plugin - best use windeployqt to deploy your app instead of copying DLLs by hand.



  • I fixed the problem by forcing a compiler and manually getting the dll from the correspondant compiler
    Thanks for the help and reactivity


Log in to reply