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.



  • Sounds like you could be interested in reading up on the [[doc:QGrapicsView]], [[doc:QGraphicsScene]], [[doc:QGraphicsItem]] and their related classes in the "Graphics View Framework":http://developer.qt.nokia.com/doc/qt-4.8/graphicsview.html .



  • 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.
    @QGraphicsScene scene(0,0,800,600);
    scene.addText("Hello, world!");
    QPen pen(Qt::blue);
    scene.addLine(0,0,200,200,pen);
    //QBrush brush();
    scene.addEllipse(400,300,100,100);
    QGraphicsView view(&scene);
    view.setGeometry(QStyle::alignedRect(
    Qt::LeftToRight,
    Qt::AlignCenter,
    w.size(),
    qApp->desktop()->availableGeometry()
    ));
    //Disable maximize button
    Qt::WindowFlags flags;
    // flags = Qt::Window | Qt::WindowMaximizeButtonHint;
    // setWindowFlags(flags);
    flags = Qt::Window | Qt::CustomizeWindowHint;
    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();@
    !http://img6.imagebanana.com/img/rb48aq71/Selection_036.png(text)!



  • 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.



  • Maybe you are using heavy weapon on your needs.
    see QPainter class doc and use simple QWidget:: paintEvent() override could be better.



  • [quote author="Andre" date="1326017055"]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. [/quote]

    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);@



  • [quote author="joonhwan" date="1326036700"]Maybe you are using heavy weapon on your needs.
    see QPainter class doc and use simple QWidget:: paintEvent() override could be better.[/quote]

    i'll try that, thanks.



  • 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[])
    {
    QApplication a(argc, argv);
    QWidget w;
    QHBoxLayout
    layout = new QHBoxLayout;
    QGraphicsView* view = new QGraphicsView;
    QGraphicsScene* scene = new QGraphicsScene;
    QImage image(800,400, QImage::Format_ARGB32);
    {
    QPainter painter(&image);
    painter.drawRect(0,0,300,200);

        painter.drawLine(QPoint(0,0), QPoint(100,100));
    
        QPen oldPen = painter.pen();
        painter.setPen(Qt::red);
        painter.drawPoint(10,50);
        painter.setPen(oldPen); // restore pen style
    
        painter.drawRect(QRect(100,200,300,50));
    }
    scene->addPixmap(QPixmap::fromImage(image));
    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)



  • It takes too much time to change a 800x600 window pixels:
    @for(int i=1;i<800;i++){
    for(int j=1;j<600;j++){
    painter.drawPoint(i,j);
    }
    scene->addPixmap(QPixmap::fromImage(image));
    view->setScene(scene);
    }
    @
    by the way, i want to show the result simultaneously. but the window freezes until showing the final result.



  • You could do

    @
    ..
    ..
    QImage image(800, 600, QImage::Format_ARGB32);
    QPainter painter(&image);
    for(int i=1;i<800;i++){
    for(int j=1;j<600;j++){
    painter.setPen(QColor(i%5, i%5, j%5));
    painter.drawPoint(i,j);
    }
    }
    scene->addPixmap(QPixmap::fromImage(image));
    view->setScene(scene);
    // ..
    // ..
    @

    calling to setScene() multiple times seems to be useless as well



  • @QImage image(800, 600, QImage::Format_ARGB32);
    QPainter painter(&image);
    // make a dark qimage
    for(int i=1;i<800;i++){
    for(int j=1;j<600;j++){
    painter.setPen(QColor(0, 0, 0));
    painter.drawPoint(i,j);
    }
    }
    scene->addPixmap(QPixmap::fromImage(image));
    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.setPen(QColor(i%5, i%5, j%5));
    painter.drawPoint(i,j);
    }
    }
    scene->addPixmap(QPixmap::fromImage(image));
    view->setScene(scene);@

    it takes some time to draw the form but i want to see the result after each iteration. is it possible?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.