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



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



  • 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 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.



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



  • 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



  • [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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.