Simple form with pixel manipulation
Hi guys,
I’m completely newbie to Qt
i want to create a 800X600 window that just show some circle and be able to manipulate pixels of the form. there is no interaction between user and form(no click, no dblclick,…) it just shows some circles with one color and lines with different pixel colors(each line may have different pixel colors)
also i want to be able to change the coordination system, i mean change it from top-left to the center of the window. could anyone help me do that with some sample code?
thanks in advance for your reply.
11 replies
Sounds like you could be interested in reading up on the QGrapicsView, QGraphicsScene, QGraphicsItem and their related classes in the Graphics View Framework [developer.qt.nokia.com] .
Thanks for reply.
Now i want to change the coordinate system but i couldn’t understand how.
this is my code, also i want to remove scroll bars.
- scene.addText("Hello, world!");
- scene.addLine(0,0,200,200,pen);
- //QBrush brush();
- scene.addEllipse(400,300,100,100);
- w.size(),
- qApp->desktop()->availableGeometry()
- ));
- //Disable maximize button
- // flags = Qt::Window | Qt::WindowMaximizeButtonHint;
- // setWindowFlags(flags);
- view.setWindowFlags(flags);
- //Set maximum and minimum size. both of them are equal, so the windows size won't change
- view.setMaximumSize(800,600);
- view.setMinimumSize(800,600);
- view.show();
Transformations are supported, and these are coordinate system changes. The scroll bars can be controlled via the QGraphicsView. Recognize that this class inherits QAbstractScrollArea, which offers the needed API to access the scroll bars.
This is the code that i try to change the coordinate system but the translate method doesn’t work! but shear and rotate are ok.
- QTransform transform;
- transform.translate(400,300);
- //transform.shear(0,1);
- //transform.rotate();
- view.setTransform(transform);
It’s simple to draw line or ellipse just by using scene.addellipse(), etc.
QGraphicsScene scene(0,0,800,600);
QGraphicsView view(&scene);
scene.addText(“Hello, world!”);
QPen pen(Qt::green);
scene.addLine(0,0,200,200,pen);
scene.addEllipse(400,300,100,100,pen);
view.show();
now what should i do to set some pixel color? may i use a widget like qimage? by the way performance is an issue for me.thanks
You could do
- int main(int argc, char *argv[])
- {
- QWidget w;
- {
- painter.drawRect(0,0,300,200);
- painter.drawPoint(10,50);
- painter.setPen(oldPen); // restore pen style
- }
- view->setScene(scene);
- layout->addWidget(view);
- w.setLayout(layout);
- w.show();
- return a.exec();
- }
. That being said, as you may notice my example, you are using heavy weapon(QGraphicsView framework). You may do easily achieve what you want by overriding QWidget::paintEvent() and drawing with QPainter(actually no big different in my example though)
- // make a dark qimage
- for(int i=1;i<800;i++){
- for(int j=1;j<600;j++){
- painter.drawPoint(i,j);
- }
- }
- view->setScene(scene);
- layout->addWidget(view);
- w.setLayout(layout);
- w.show();
- for(int i=1;i<8000;i++){
- for(int j=1;j<600;j++){
- painter.drawPoint(i,j);
- }
- }
- view->setScene(scene);
it takes some time to draw the form but i want to see the result after each iteration. is it possible?
You must log in to post a reply. Not a member yet? Register here!


