Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple form with pixel manipulation
Qt 6.11 is out! See what's new in the release blog

Simple form with pixel manipulation

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 7.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mohsenaria
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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 .

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohsenaria
        wrote on last edited by
        #3

        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)!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            joonhwan
            wrote on last edited by
            #5

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

            joonhwan at gmail dot com

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mohsenaria
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mohsenaria
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mohsenaria
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    joonhwan
                    wrote on last edited by
                    #9

                    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)

                    joonhwan at gmail dot com

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mohsenaria
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        joonhwan
                        wrote on last edited by
                        #11

                        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

                        joonhwan at gmail dot com

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mohsenaria
                          wrote on last edited by
                          #12

                          @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?

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved