Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why can't I see any GUI?
Qt 6.11 is out! See what's new in the release blog

Why can't I see any GUI?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jonas25
    wrote on last edited by
    #1

    I tried to write a program without the help of qtcreator but only command line tools + vim. My problem now is, that there is no gui, and I can't find my mistake:

    backgroundWorker.hpp

    #include <QObject>
    #include <QPushButton>
    
    class BackgroundWorker : public QWidget{
    
        Q_OBJECT
    
        public:
            explicit BackgroundWorker();
    
            private slots:
                void start();
    
        private:
            QPushButton* mStartButton;
    };
    

    backgroundWorker.cpp

    #include <iostream>
    #include "getInput.hpp"
    #include "backgroundWorker.hpp"
    #include "LIF_network.hpp"
    
    BackgroundWorker::BackgroundWorker(){
        mStartButton = new QPushButton("Start",this);
        mStartButton->setGeometry(QRect(QPoint(100,100),QSize(200,50)));
    
        connect(mStartButton, SIGNAL(released()), this, SLOT(start()));
        //start();
    }
    
    void BackgroundWorker::start(){
        //main code, qt used to visualize progress
    }
    

    main.cpp

    #include <QApplication>
    #include "backgroundWorker.hpp"
    
    int main(int argc, char *argv[]){
        QApplication a(argc, argv);
        BackgroundWorker bw;
        return a.exec();
    }
    

    There should be a button shown, but it is not. In the start() method I draw a QPixmap in qt and show it simply on a QLabel. If I call the start function already in the constructor The QLabel is shown on the screen as a normal window, but there is nothing in the window, but just a part of my desktop background. What am I doing wrong?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi, I think you need to add bw.show(); to main.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jonas25
        wrote on last edited by
        #3

        Thank you, now the button in the beginning is showing, but my problem with the QLabels that don't draw anything inside still remains, can you might help me? I need to show a QPixmap filled with Rectangles, but the programme opens a new window, but does not draw anything inside.

        Here is my code:

        void BackgroundWorker::start(){
            int pixelSize = 1;
            //read input data
            int width=0;
            int height=0;
            std::vector<double>bmp = readBMP("/home/jonas/LIF_neural_network_image_recognition/images/plain_landscape_tree.bmp", &width, &height);
        
            //draw the first window - there is nothing drawn in until the whole start() method ended
            mImages.push_back(new Image(pixelSize,width, height, bmp, "Original"));
        
            //create the network
            // takes a lot of time
            LIF_network network(width, height);
            network.feed(bmp);
            network.step()
        
            //draw result
            std::vector<double> result =  network.getOutput();
        
            mImages.push_back(new Image(pixelSize, width, height, result, "Result"));
        }
        

        images.hpp

        class Image{
            public:
                Image(int tileSize, int sizeX, int sizeY, std::vector<double> values);
                ~Image();
                void close();
        
            private:
                int mSizeX, mSizeY, mTileSize;
                QLabel* mLabel;
                QPixmap mPixmap;
        
                QPixmap drawPixmap(std::vector<double> values);
                std::vector<QRect> mRectangles;
                void initializeRectangles();
        };
        
        

        images.cpp

        Image::Image(int tileSize, int sizeX, int sizeY, std::vector<double> values):
            mSizeX(sizeX), mSizeY(sizeY), mTileSize(tileSize)
        {
            initializeRectangles();
            mPixmap = QPixmap(sizeX*mTileSize, sizeY*mTileSize);
            mLabel = new QLabel("no title");
            mLabel->setPixmap(drawPixmap(values));
            mLabel->show();
        }
        
        void Image::initializeRectangles(){
            mRectangles = std::vector<QRect>();
            for(int y=0; y<mSizeY; ++y){
                for(int x=0; x<mSizeX; ++x){
                    QRect rec(x*mTileSize,y*mTileSize,mTileSize,mTileSize);
                    mRectangles.push_back(rec);
                }
            }
        }
        
        Image::~Image(){
            delete mLabel;
        }
        
        QPixmap Image::drawPixmap(std::vector<double> values){
            mPixmap.fill(Qt::red);
            QPainter p(&mPixmap);
            //QPen pen = QPen(Qt::black, 2);
            p.setPen(Qt::NoPen);
            //p.setRenderHint(QPainter::Antialiasing, true);
            QBrush brush(Qt::blue);
        
            //draw every rectangle in its color
            for(size_t i=0; i<values.size(); ++i){
                int colVal = floor(values[i]*255);
                //std::cout << values[i] << std::endl;
                brush.setColor(QColor(colVal,colVal,colVal));
                p.setBrush(brush);
                p.drawRect(mRectangles[i]);
            }
            return mPixmap;
        }
        
        void Image::close(){
            mLabel->close();
        }
        

        Thank you a lot!

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @Jonas25 said:

          What happens when you comment out the following line:
          mLabel->setPixmap(drawPixmap(values)); ?
          Does the the label then show the text "no title"?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jonas25
            wrote on last edited by Jonas25
            #5

            The title is displayed correct, there is just no content in the window. I will upload screenshots:
            after starting the first window but during the computations
            after the computations

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #6

              I'm not completely sure because I can't see all of your code but I think that your application spends a lot of time on some computations in the main thread and thus the GUI doesn't get repainted (freezes). You need to make sure that the main thread (the one that executes the main event loop (return a.exec();)) doesn't get blocked. So you'll have to move your heavy computations to another thread. Take a look at the Mandelbrot example: http://doc.qt.io/qt-5/qtcore-threads-mandelbrot-example.html

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved