标题:我就想画条线,程序写出来,编译通过,但运行结果不对!(附程序)
作者:yangfan135
日期:2006-05-26 09:11
内容:
程序如下:
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
QApplication a( argc, argv );
QWidget m;
QCanvas canvas( 800, 600 );
canvas.setAdvancePeriod(30);
a.setMainWidget(&m);
QCanvasLine* i = new QCanvasLine(&canvas);
i->setPoints( rand()%canvas.width(), rand()%canvas.height(),
rand()%canvas.width(), rand()%canvas.height() );
i->setPen( QPen(QColor(rand()%32*8, rand()%32*8, rand()%32*8), 6) );
i->setZ(rand()%256);
i->show();
m.show();
return a.exec();
}
我的QT版本是3.3,运行的结果是只出现一个大白框,里面却什么也没有!哪位高人请指点,谢谢!!!
#1 [myer 05-26 09:36]
应该new canvasview,然后setcanvas();
同时,new的canvas item初始好像都是hided状态,应该show()一下。
#2 [yangfan135 05-26 10:11]
我是新手,不太明白你的意思!能详细些吗?或者具体程序应该如何改?
#3 [myer 05-26 11:55]
QWidget m;
改为QCavasView m;m.setCanvas(&canvas);
再试试。
#4 [yangfan135 05-26 14:32]
这是我改后的程序:
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
QApplication a( argc, argv );
// QWidget m;
QCanvas canvas( 800, 600 );
canvas.setAdvancePeriod(30);
QCavasView m;
m.setCanvas(&canvas);
a.setMainWidget(&m);
QCanvasLine* i = new QCanvasLine(&canvas);
i->setPoints( rand()%canvas.width(), rand()%canvas.height(),
rand()%canvas.width(), rand()%canvas.height() );
i->setPen( QPen(QColor(rand()%32*8, rand()%32*8, rand()%32*8), 6) );
i->setZ(rand()%256);
i->show();
m.show();
return a.exec();
}
错误信息如下:
test.cpp: In function `int main(int, char**)':
test.cpp:20: error: `QCavasView' was not declared in this scope
test.cpp:20: error: expected `;' before "m"
test.cpp:21: error: `m' was not declared in this scope
怎么回事那?
#5 [yangfan135 05-26 15:13]
成功了