January 4, 2012

mohsenaria mohsenaria
Lab Rat
7 posts

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

January 4, 2012

Andre Andre
Area 51 Engineer
6031 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

January 8, 2012

mohsenaria mohsenaria
Lab Rat
7 posts

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.

  1. QGraphicsScene scene(0,0,800,600);
  2.     scene.addText("Hello, world!");
  3.     QPen pen(Qt::blue);
  4.     scene.addLine(0,0,200,200,pen);
  5.     //QBrush brush();
  6.     scene.addEllipse(400,300,100,100);
  7.     QGraphicsView view(&scene);
  8.     view.setGeometry(QStyle::alignedRect(
  9.                          Qt::LeftToRight,
  10.                          Qt::AlignCenter,
  11.                          w.size(),
  12.                          qApp->desktop()->availableGeometry()
  13.                      ));
  14.     //Disable maximize button
  15.     Qt::WindowFlags flags;
  16. //    flags = Qt::Window | Qt::WindowMaximizeButtonHint;
  17. //    setWindowFlags(flags);
  18.     flags = Qt::Window | Qt::CustomizeWindowHint;
  19.     view.setWindowFlags(flags);
  20.     //Set maximum and minimum size. both of them are equal, so the windows size won't change
  21.     view.setMaximumSize(800,600);
  22.     view.setMinimumSize(800,600);
  23.     view.show();

text

January 8, 2012

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

January 8, 2012

joonhwan joonhwan
Lab Rat
94 posts

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

 Signature 

joonhwan at gmail dot com

January 10, 2012

mohsenaria mohsenaria
Lab Rat
7 posts
Andre wrote:
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.

  1. QTransform transform;
  2.     transform.translate(400,300);
  3.     //transform.shear(0,1);
  4.     //transform.rotate();
  5.     view.setTransform(transform);

January 10, 2012

mohsenaria mohsenaria
Lab Rat
7 posts
joonhwan wrote:
Maybe you are using heavy weapon on your needs. see QPainter class doc and use simple QWidget:: paintEvent() override could be better.

i’ll try that, thanks.

February 4, 2012

mohsenaria mohsenaria
Lab Rat
7 posts

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

February 6, 2012

joonhwan joonhwan
Lab Rat
94 posts

You could do

  1. int main(int argc, char *argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.     QWidget w;
  5.     QHBoxLayout* layout = new QHBoxLayout;
  6.     QGraphicsView* view = new QGraphicsView;
  7.     QGraphicsScene* scene = new QGraphicsScene;
  8.     QImage image(800,400, QImage::Format_ARGB32);
  9.     {
  10.         QPainter painter(&image);
  11.         painter.drawRect(0,0,300,200);
  12.  
  13.         painter.drawLine(QPoint(0,0), QPoint(100,100));
  14.  
  15.         QPen oldPen = painter.pen();
  16.         painter.setPen(Qt::red);
  17.         painter.drawPoint(10,50);
  18.         painter.setPen(oldPen); // restore pen style
  19.  
  20.         painter.drawRect(QRect(100,200,300,50));
  21.     }
  22.     scene->addPixmap(QPixmap::fromImage(image));
  23.     view->setScene(scene);
  24.  
  25.     layout->addWidget(view);
  26.     w.setLayout(layout);
  27.     w.show();
  28.     return a.exec();
  29. }

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

 Signature 

joonhwan at gmail dot com

February 7, 2012

mohsenaria mohsenaria
Lab Rat
7 posts

It takes too much time to change a 800×600 window pixels:

  1. for(int i=1;i<800;i++){
  2.         for(int j=1;j<600;j++){
  3.             painter.drawPoint(i,j);
  4.         }
  5.         scene->addPixmap(QPixmap::fromImage(image));
  6.         view->setScene(scene);
  7.     }

by the way, i want to show the result simultaneously. but the window freezes until showing the final result.

February 7, 2012

joonhwan joonhwan
Lab Rat
94 posts

You could do

  1. ..
  2. ..
  3. QImage image(800, 600, QImage::Format_ARGB32);
  4. QPainter painter(&image);
  5. for(int i=1;i<800;i++){
  6.         for(int j=1;j<600;j++){
  7.             painter.setPen(QColor(i%5, i%5, j%5));
  8.             painter.drawPoint(i,j);
  9.         }
  10.     }
  11. scene->addPixmap(QPixmap::fromImage(image));
  12. view->setScene(scene);
  13. // ..
  14. // ..

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

 Signature 

joonhwan at gmail dot com

February 13, 2012

mohsenaria mohsenaria
Lab Rat
7 posts

  1. QImage image(800, 600, QImage::Format_ARGB32);
  2.     QPainter painter(&image);
  3.     // make a dark qimage
  4.     for(int i=1;i<800;i++){
  5.             for(int j=1;j<600;j++){
  6.                 painter.setPen(QColor(0, 0, 0));
  7.                 painter.drawPoint(i,j);
  8.             }
  9.         }
  10.     scene->addPixmap(QPixmap::fromImage(image));
  11.     view->setScene(scene);
  12.     layout->addWidget(view);
  13.     w.setLayout(layout);
  14.     w.show();
  15.     for(int i=1;i<8000;i++){
  16.             for(int j=1;j<600;j++){
  17.                 painter.setPen(QColor(i%5, i%5, j%5));
  18.                 painter.drawPoint(i,j);
  19.             }
  20.         }
  21.     scene->addPixmap(QPixmap::fromImage(image));
  22.     view->setScene(scene);

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

 
  ‹‹ Merging two columns using QSqlTableModel/QTableView      [Solved] Memory freed but stays reserved... why? ››

You must log in to post a reply. Not a member yet? Register here!