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. [SOLVED]QWidget: Cannot create a QWidget when no GUI is being used
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QWidget: Cannot create a QWidget when no GUI is being used

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 11.4k 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.
  • N Offline
    N Offline
    nimingzhe2008
    wrote on 4 Nov 2012, 12:54 last edited by
    #1

    When I ran my code,no widget appeared and IDE showed me the message "QWidget: Cannot create a QWidget when no GUI is being used" . I don't know why.Here's my code

    tooltipzone_mine.pro
    @#-------------------------------------------------

    Project created by QtCreator 2012-11-03T21:39:57

    #-------------------------------------------------

    QT += core
    QT += gui

    TARGET = tooltipzone_mine

    TEMPLATE = app

    SOURCES += main.cpp
    tipzone.cpp

    HEADERS +=
    tipzone.h
    @

    tipzone.h
    @#ifndef TIPZONE_H
    #define TIPZONE_H
    #include<QWidget>

    class tipzone:public QWidget
    {
    public:
    tipzone();
    void paintEvent( QPaintEvent* );
    };

    #endif // TIPZONE_H@

    tipzone.cpp
    @#include "tipzone.h"

    #include<QPainter>
    #include<QPaintEvent>
    #include<QRect>

    tipzone::tipzone():QWidget()
    {
    }

    void tipzone::paintEvent(QPaintEvent *)
    {
    QRect redRect, greenRect, blueRect, yellowRect;

    redRect = QRect( 0, 0, width()/2, height()/2 );
    greenRect = QRect( width()/2, 0, width()/2, height()/2 );
    blueRect = QRect( 0, height()/2, width()/2, height()/2 );
    yellowRect = QRect( width()/2, height()/2, width()/2, height()/2 );
    
    QPainter p( this );
    
    p.setPen( Qt::black );
    
    p.setBrush( Qt::red );
    p.drawRect( redRect );
    
    p.setBrush( Qt::green );
    p.drawRect( greenRect );
    
    p.setBrush( Qt::blue );
    p.drawRect( blueRect );
    
    p.setBrush( Qt::yellow );
    p.drawRect( yellowRect );
    

    }

    @

    main.cpp
    @#include <QtCore/QCoreApplication>
    #include"tipzone.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    tipzone t;
    t.show();
    return a.exec();
    }
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Code_ReaQtor
      wrote on 4 Nov 2012, 13:50 last edited by
      #2

      I think the problem with your code was that

      @void paintEvent( QPaintEvent* );@

      is a [virtual protected] function, "QWidget":http://qt-project.org/doc/qt-4.8/qwidget.html#paintEvent

      You may try this to your header file:

      @#ifndef TIPZONE_H
      #define TIPZONE_H
      #include<QWidget>

      class tipzone:public QWidget
      {
      public:
      tipzone();

      protected:
      void paintEvent( QPaintEvent* );
      };

      #endif // TIPZONE_H@

      Please visit my open-source projects at https://github.com/Code-ReaQtor.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nimingzhe2008
        wrote on 4 Nov 2012, 14:28 last edited by
        #3

        [quote author="Code_ReaQtor" date="1352037059"]I think the problem with your code was that

        @void paintEvent( QPaintEvent* );@

        is a [virtual protected] function, "QWidget":http://qt-project.org/doc/qt-4.8/qwidget.html#paintEvent

        You may try this to your header file:

        @#ifndef TIPZONE_H
        #define TIPZONE_H
        #include<QWidget>

        class tipzone:public QWidget
        {
        public:
        tipzone();

        protected:
        void paintEvent( QPaintEvent* );
        };

        #endif // TIPZONE_H@[/quote]

        Thank you,but it doesn't work.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nimingzhe2008
          wrote on 4 Nov 2012, 14:49 last edited by
          #4

          I got it.I should use QApplication instead of QCoreApplication in main.cpp.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuhamedAuda
            wrote on 4 Nov 2012, 15:17 last edited by
            #5

            Dear nmingzhe2008
            it easy to build this project if u used Qt Creator wizard

            just start new project.

            type class name : tipzone

            and base class: Qwidget

            and then add ur own code

            @#-------------------------------------------------

            Project created by QtCreator 2012-11-04T17:03:28

            #-------------------------------------------------

            QT += core gui

            TARGET = untitled3
            TEMPLATE = app

            SOURCES += main.cpp
            tipzone.cpp

            HEADERS += tipzone.h
            @

            @//// tipzone.h
            #ifndef TIPZONE_H
            #define TIPZONE_H

            #include <QtGui/QWidget>

            class tipzone : public QWidget
            {
            Q_OBJECT

            public:
            tipzone(QWidget *parent = 0);
            // just i add paintEvent only
            void paintEvent(QPaintEvent *);
            ~tipzone();
            };

            #endif // TIPZONE_H
            @

            @////////////// tipzone.cpp

            #include "tipzone.h"
            // and added ur includefiles
            #include<QPainter>
            #include<QPaintEvent>
            #include<QRect>

            tipzone::tipzone(QWidget *parent)
            : QWidget(parent)
            {
            }

            tipzone::~tipzone()
            {
            }
            // now just implementation for pain ...
            void tipzone::paintEvent(QPaintEvent *e)
            {
            // now i added ur code
            QRect redRect, greenRect, blueRect, yellowRect;

            redRect = QRect( 0, 0, width()/2, height()/2 );
            greenRect = QRect( width()/2, 0, width()/2, height()/2 );
            blueRect = QRect( 0, height()/2, width()/2, height()/2 );
            yellowRect = QRect( width()/2, height()/2, width()/2, height()/2 );
            QPainter p( this );
            p.setPen( Qt::black );
            p.setBrush( Qt::red );
            p.drawRect( redRect );
            p.setBrush( Qt::green );
            p.drawRect( greenRect );
            p.setBrush( Qt::blue );
            p.drawRect( blueRect );
            p.setBrush( Qt::yellow );
            p.drawRect( yellowRect );
            

            }
            @

            @/////////main.cpp

            #include <QtGui/QApplication>
            #include "tipzone.h"

            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv);
            tipzone w;
            w.show();
            return a.exec();
            }
            @

            the out put divided the main windown into four rectangle colored in Red, Green, Blue and Yallow colos nice code
            Muhamed Auda

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Macro
              wrote on 5 Nov 2012, 10:42 last edited by
              #6

              [quote author="nimingzhe2008" date="1352040548"]I got it.I should use QApplication instead of QCoreApplication in main.cpp.[/quote]

              Yeah That's Correct. QCoreApplication is used for non GUI Applications. For more details check this "QCoreApplication Class Reference.":http://qt-project.org/doc/qt-4.8/qcoreapplication.html#details

              1 Reply Last reply
              0

              1/6

              4 Nov 2012, 12:54

              • Login

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