#ifndef MARKBUTTON_H#define MARKBUTTON_H#include <QObject>#include <QPushButton>class MarkButton : public QPushButton{ // Q_OBJECT // -- 1public: MarkButton(QWidget* parent = 0);public slots: void click();protected: void paintEvent(QPaintEvent* event);};#endif // MARKBUTTON_H
#include "MarkButton.h"#include <QPainter>MarkButton::MarkButton(QWidget* parent) : QPushButton(parent){ resize(50,50); QObject::connect(this, SIGNAL(clicked()), this, SLOT(click())); // -- 2}void MarkButton::paintEvent(QPaintEvent* event){ QPainter painter(this); painter.setPen ( Qt::red ); painter.drawRect( QRect(0,0, 50, 50) );}void MarkButton::click(){ int x = 5; x++; x++; // тут брейкпоинт x++; int y = x;}
void f(QWidget* buttonPlace){ m_linkedButton = new MarkButton(buttonPlace);}
#ifndef BUTTONCREATOR_H#define BUTTONCREATOR_H#include <QObject>#include <QGraphicsItem>class ButtonCreator : public QObject, public QGraphicsItem{ Q_OBJECTpublic: ButtonCreator(QWidget* parent = 0); QRectF boundingRect() const { return QRectF(); } void paint(QPainter* p, const QStyleOptionGraphicsItem* o, QWidget* w) { }public slots: void onClick();private: QWidget* m_parent;};#endif // BUTTONCREATOR_H
#include "buttoncreator.h"#include <QMessageBox>#include <QPushButton>ButtonCreator::ButtonCreator(QWidget* parent) : m_parent(parent){ QPushButton* button = new QPushButton("Dynamic buton", parent); button->setVisible(true); button->setEnabled(true); QObject::connect(button, SIGNAL(clicked()), this, SLOT(onClick()));}void ButtonCreator::onClick(){ QMessageBox::information(m_parent, "123", "123");}