Attention question changed (added a piece of code)
Hello!
How to create a custom widget in the form of a form, and then you could move it around the form. Those. there is a form, and inside it there is a widget that moves inside the form. And there, for example, he has LineEdit, PushButton
.
It is possible through the drawline
, but it does not roll like that, I need the widget to move while holding the left mouse button
Sample form:
If it is not possible, then how is this area to be moved, via mouse pressed
?
#include <QtGui> int main(int argc, char *argv[]) { QApplication a(argc, argv); QGraphicsScene scene(QRectF(-100, -100, 300, 300)); QGraphicsView view(&scene); QTextEdit* text = new QTextEdit(); text->setFixedSize(100,30); text->setGeometry(-40,-70,100,30); QGraphicsProxyWidget* widgetItem = scene.addWidget(text); widgetItem->setFlag(QGraphicsItem::ItemIsMovable, true);//Π’Π°ΠΊ Π½Π΅ ΠΏΠ΅ΡΠ΅Π΄Π²ΠΈΠ³Π°Π΅ΡΡΡ ΠΏΠΎ ΡΠΎΡΠΌΠ΅ QPushButton cmd("Button"); widgetItem = scene.addWidget(&cmd); widgetItem->setFlag(QGraphicsProxyWidget::ItemIsMovable); QGraphicsRectItem* rect = new QGraphicsRectItem(0, &scene); rect->setBrush(QBrush(Qt::gray)); rect->setRect(QRectF(-20, -20, 120, 70)); rect->setFlags(QGraphicsItem::ItemIsMovable); view.show(); return a.exec(); }
It turns out you have to add a mouse pressed event? Or, as in the example of alexeinaumov , add the class MyWidget?
New question . The campaign that the button is pressed or edited cannot be moved. Whereas, it can be done so that, inside some area (the area we will have to specify through QGraphicsItem) there was a button (QGraphicsProxyWidget) and it (using QGraphicsItem) moved, i.e. is the button inside the area and using the area moving?
Note: QWidget-based widgets can be directly embedded into a QGraphicsScene using QGraphicsProxyWidget.
- progzdeveloper 6:24 pmQGraphicsRectItem* rect = new QGraphicsRectItem(0, &scene); rect->setBrush(QBrush(Qt::gray)); rect->setRect(QRectF(-20, -20, 120, 70)); rect->setFlags(QGraphicsItem::ItemIsMovable); QPushButton cmd("Button"); QGraphicsProxyWidget *btnProxy = new QGraphicsProxyWidget(rect, &scene); // btnProxy ΡΠ΅ΠΏΠ΅ΡΡ Π΄ΠΎΡΠ΅ΡΠ½ΠΈΠΉ Π΄Π»Ρ rect btnProxy->setWidget(&cmd); btnProxy->setFlag(QGraphicsProxyWidget::ItemIsMovable);
QGraphicsRectItem* rect = new QGraphicsRectItem(0, &scene); rect->setBrush(QBrush(Qt::gray)); rect->setRect(QRectF(-20, -20, 120, 70)); rect->setFlags(QGraphicsItem::ItemIsMovable); QPushButton cmd("Button"); QGraphicsProxyWidget *btnProxy = new QGraphicsProxyWidget(rect, &scene); // btnProxy ΡΠ΅ΠΏΠ΅ΡΡ Π΄ΠΎΡΠ΅ΡΠ½ΠΈΠΉ Π΄Π»Ρ rect btnProxy->setWidget(&cmd); btnProxy->setFlag(QGraphicsProxyWidget::ItemIsMovable);
- progzdeveloper