#include "widget.h"
#include <QHBoxLayout>
#include <QComboBox>
#include <QLabel>
#include <QDebug>
#include <QPainter>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
paintArea = new QWidget(this);
paintArea->setMinimumSize(400,400);
paintArea->setStyleSheet("border: 2px solid black;"); // 设置边框样式
QLabel *comboName = new QLabel("形状:",this);
comboShape = new QComboBox(this);
comboShape->addItem("Line",Line);
comboShape->addItem("Rectangle",Rectangle);
QHBoxLayout *comboLayout = new QHBoxLayout;
comboLayout->addWidget(comboName);
comboLayout->addWidget(comboShape);
QHBoxLayout *hLayout = new QHBoxLayout(this);
hLayout->addWidget(paintArea);
hLayout->addLayout(comboLayout);
connect(comboShape,SIGNAL(activated(int)),this,SLOT(do_shapeUpdate(int)));
do_shapeUpdate(comboShape->currentIndex());
}
void Widget::do_shapeUpdate(int index){
shp = shape(comboShape->itemData(index,Qt::UserRole).toInt());
update();
}
void Widget::paintEvent(QPaintEvent *){
QPainter p(this);
// QPainter p(paintArea);这一行就不会显示绘制的形状?为什么呢
p.setPen(QPen(Qt::blue));
p.setBrush(QBrush(Qt::black));
QRect rect(55,110,290,180);
qDebug() << "1111";// 添加调试输出
switch(shp){
case Line:
p.drawLine(rect.topLeft(),rect.bottomRight());
break;
case Rectangle:
p.drawRect(rect);
break;
default:
break;
}
}
Widget::~Widget()
{
}
#include <QHBoxLayout>
#include <QComboBox>
#include <QLabel>
#include <QDebug>
#include <QPainter>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
paintArea = new QWidget(this);
paintArea->setMinimumSize(400,400);
paintArea->setStyleSheet("border: 2px solid black;"); // 设置边框样式
QLabel *comboName = new QLabel("形状:",this);
comboShape = new QComboBox(this);
comboShape->addItem("Line",Line);
comboShape->addItem("Rectangle",Rectangle);
QHBoxLayout *comboLayout = new QHBoxLayout;
comboLayout->addWidget(comboName);
comboLayout->addWidget(comboShape);
QHBoxLayout *hLayout = new QHBoxLayout(this);
hLayout->addWidget(paintArea);
hLayout->addLayout(comboLayout);
connect(comboShape,SIGNAL(activated(int)),this,SLOT(do_shapeUpdate(int)));
do_shapeUpdate(comboShape->currentIndex());
}
void Widget::do_shapeUpdate(int index){
shp = shape(comboShape->itemData(index,Qt::UserRole).toInt());
update();
}
void Widget::paintEvent(QPaintEvent *){
QPainter p(this);
// QPainter p(paintArea);这一行就不会显示绘制的形状?为什么呢
p.setPen(QPen(Qt::blue));
p.setBrush(QBrush(Qt::black));
QRect rect(55,110,290,180);
qDebug() << "1111";// 添加调试输出
switch(shp){
case Line:
p.drawLine(rect.topLeft(),rect.bottomRight());
break;
case Rectangle:
p.drawRect(rect);
break;
default:
break;
}
}
Widget::~Widget()
{
}
