drawrect qt
时间: 2023-10-12 22:06:18 浏览: 36
Sure! I can help you with drawing a rectangle in Qt.
In, you can use the QPainter class to draw shapes on a widget or a pixmap. To draw a rectangle, you can use the drawRect() method of the QPainter class.
Here is an example of how you can draw a rectangle using Qt:
```cpp
#include <QApplication>
#include <QPainter>
#include <QWidget>
class MyWidget : public QWidget {
public:
void paintEvent(QPaintEvent *)
{
QPainter painter(this);
// Set the pen color and width
painter.setPen(QPen(Qt::blue, 2));
// Set the brush color
painter.setBrush(QBrush(Qt::yellow));
// Draw a rectangle with top-left corner at (50, 50), and width = 200, height = 100
painter.drawRect(50, 50, 200, 100);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
```
In this example, we create a custom QWidget subclass called `MyWidget` and override the `paintEvent()` function to perform our custom drawing operations. Inside the `paintEvent()` function, we create a QPainter object and set the pen and brush properties for our rectangle. Finally, we call the `drawRect()` method to draw the rectangle.
I hope this helps you get started with drawing rectangles in Qt! Let me know if you have any further questions.