关于qt paintevent 的问题

癫狂症患者 2017-06-06 11:02:38
程序功能是下围棋,目前棋盘画出来了,但棋子不能下
我定义了一个棋盘类 继承的Qlabel
class ChessBoard : public QLabel
{
Q_OBJECT
public:
explicit ChessBoard(size_t row,size_t column,QWidget *parent);
bool draw_board();
signals:
void clicked(size_t, size_t);

public slots:
protected:
void mouseReleaseEvent(QMouseEvent *ev);
~ChessBoard(){}
private:
size_t row;
size_t column;

static const size_t padding = 20;
static const size_t interval = 35;
};

在主界面类中 我将这个棋盘类加到主界面的布局里了
QGridLayout *layout = new QGridLayout();
layout->addWidget(board,0,0,1,1);
layout->addWidget(ui->go_exit,0,1,1,1);
layout->takeAt(0);
qDebug()<<
this->setLayout(layout);


然后在paintevent 中绘制棋子
void clientWidget::paintEvent(QPaintEvent *event)
{
ui->stackedWidget->currentWidget()->layout();
if(game != GO && game!=FIVE) {
// qDebug()<<"dag";
return;
}
//qDebug()<<"paint";
int GridSize=(height()-60)/18;

QPainter p(this);//感觉是这里出了问题,应该画到棋盘上才对,我不会弄。。。
QPen pen;
QBrush brush;

pen.setWidth(2);
pen.setColor(Qt::black);
p.setPen(pen);

//画棋子
int size;
if(game==GO){
size=Go->boardSize();
}else if(game==FIVE){
size=Five->boardSize();
}

for(int i=0;i<size ;i++){
for(int j=0;j<size ;j++){
StoneType current;
if(game==GO){
current= Go->checkStone(i,j);
}else if(game==FIVE){
current= Five->checkStone(i,j);
}
if(current==BLACK){
brush.setColor(Qt::red);
p.setBrush(brush);
p.drawEllipse(QPoint((i)*GridSize+30,(j)*GridSize+30),12,12);
}
else if(current==WHITE){
brush.setColor(Qt::white);
p.setBrush(brush);
p.drawEllipse(QPoint((i)*GridSize+30,(j)*GridSize+30),12,12);
}
}
}
this->children()

}

鼠标点击事件加了update();
根本没有反应 画不上去 ,调试了paintevent 进去了 没有画到主界面上
是不是这个paintevent 应该加到 棋盘类里面???
求大神解答一下 !!!
谢谢!!!!
...全文
212 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_38755299 2017-06-07
  • 打赏
  • 举报
回复
jklinux 2017-06-06
  • 打赏
  • 举报
回复
窗口对象上面应是被标签(棋盘)挡住了,paintEvent应是写在棋盘类里

16,212

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧