0

I am trying to create a custom widgetclass which inherits from QWidget. The problem is, that as I add the Q_OBJECT macro to my custom class, the widget doesn't get displayed (no erros). If I remove the macro run qmake und run the project again, the customwidget is shown. I am used to add the Q_Object macro because I want to add signals and slots.

Widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>


class Widget : public QWidget
{
    Q_OBJECT

    public:
        Widget(QWidget *parent = 0);

};

#endif // WIDGET_H

Widget.cpp:

#include "Widget.h"

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

main.cpp:

#include "Widget.h"

#include <QApplication>
#include <QWidget>
#include <QPoint>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget window_main;


    Widget *widget_steps = new Widget(&window_main);
    widget_steps->setFixedWidth(100);
    widget_steps->setFixedHeight(100);
    widget_steps->move(QPoint(0, 0));
    widget_steps->setStyleSheet("background-color: red");


    window_main.showMaximized();

    return a.exec();
}

Without Q_OBJECT macro a red rect is shown on the upper-left corner, if I add the macro, it isn't shown. I am using Qt 5.1 and Windows 7.

I am new to Qt, so please keep your answers simple :)

user1655806
  • 396
  • 1
  • 3
  • 15
  • I'm not sure what u mean, but when I run the project, Qt-Creator gives me that text: "QWindowsWindow::setGeometry: Unable to set geometry 100x100+363+124 on 'QWidgetClassWindow'. Resulting geometry: 116x100+363+124 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215)." – user1655806 Aug 30 '13 at 01:01
  • That was what I was looking for. Although this message does not help me. – drescherjm Aug 30 '13 at 01:12
  • 4
    This question is answered here: http://stackoverflow.com/questions/18344135/why-do-stylesheets-not-work-when-subclassing-qwidget-and-using-q-object. But, I would not call it [duplicate]. Hard to impossible to find if you haven't seen it a few days ago. – Greenflow Aug 30 '13 at 01:12
  • 1000 Thanks to you Greenflow! After 4hours of try and error and googling it finally works! :) – user1655806 Aug 30 '13 at 01:48

0 Answers0