Results 1 to 8 of 8

Thread: problem in drwing several images on screen

  1. #1
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default problem in drwing several images on screen

    Hi everybody,
    I'm just a new to qt!

    I'm going to implement a project, that I need to draw an unknown number of images on screen.
    I tried 3 methods, but none of them worked.

    1.
    I tested QLabel for drawing a picture by the use of "pixmap".
    As I said I need to paint an unknown number of images.[n*m, e.g. 2 or 6,...]

    I tried like this... with the thought that this will add several(here 2) labels(pix) to my MainWindow:

    Qt Code:
    1. QPixmap *pixmap_and = new QPixmap("../pix/AND.png");
    2. QLabel *label_1 = new QLabel(this);
    3. label_1->setPixmap(*pixmap_and);
    4. label_1->adjustSize();
    5. label_1->move(100,100);
    6.  
    7. QLayout *layout_1 = this->layout();
    8. layout_1->addWidget( label_1 );
    9. label_1->move(200,200);
    10. layout_1->addWidget( label_1 );
    11.  
    12. this->setLayout(layout_1);
    To copy to clipboard, switch view to plain text mode 

    I doesn't work!
    I found that for drawing several images using QLabels I need to create several number of QLabels.
    I tried to use a "vector" of "QLable". Unfortunately I couldn't do it because of several syntax problems.
    would you guide me how can I implement that? (If it is possible!)

    2.
    I also tried QImage and QPainter.
    I implemented a code like this :

    Qt Code:
    1. QImage image("../pix/AND.png");
    2. if (!image.isNull())
    3. ui->label->setText("Image Loaded! ");
    4. QPainter qpainter(this);
    5. qpainter.drawImage(100,100,image);
    6.  
    7. image.load(":/pix/AND.png");
    8. qpainter.drawImage(100,100,image);
    To copy to clipboard, switch view to plain text mode 


    As you see I used both pix in my hard and Qt resources. The image is sucsessfuly loaded. But I dont see anything
    on screen. Is there any problem in my code?


    3. I also tried this:
    Qt Code:
    1. QPixmap pixmap_tmp;
    2. pixmap_tmp.load("../pix/AND.png");
    3. qpainter.drawPixmap(200,200,50,50,pixmap_tmp);
    4.  
    5. pixmap_tmp.load(":/pix/AND.png");
    6. qpainter.drawPixmap(200,200,50,50,pixmap_tmp);
    To copy to clipboard, switch view to plain text mode 

    again I didn't see anything on screen. ?!


    Thanks in advance for further helps,

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem in drwing several images on screen

    I think what You need is, QStandardItemModel that stores images, and ListView to display them.
    You can scale QPixmap to fit display area, and implement click or double click on the List to display full size. ( <- I assume that You want to display i.e. 24 images, that obviously won't fit on the screen).
    If that's not the case, then here ( qLabExample..zip ) is an example that displays up to 4 images in 4 QLabels, code is quick draft and not perfect, but You will get the idea.

    Best luck

  3. The following user says thank you to Talei for this useful post:

    d@nyal (29th April 2010)

  4. #3
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: problem in drwing several images on screen

    Quote Originally Posted by Talei View Post
    I think what You need is, QStandardItemModel that stores images, and ListView to display them.
    You can scale QPixmap to fit display area, and implement click or double click on the List to display full size. ( <- I assume that You want to display i.e. 24 images, that obviously won't fit on the screen).
    If that's not the case, then here ( qLabExample..zip ) is an example that displays up to 4 images in 4 QLabels, code is quick draft and not perfect, but You will get the idea.

    Best luck
    Hi Talei,
    Thanks for reply!

    The example was clear and I got the general idea...
    -----
    In your program your inserted images into a "QList" and setted them to "a" Qlabel by the use of a pixmap. It automatically showed them vertically. If I want to show some of them vertically, what shall i do? (see the picture to get my intention)

    Thanks,

  5. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem in drwing several images on screen

    I guess that is a homework, and I saw that You are student of electrical engineering, so due to sentiment of the old days at uni (same department ), I prepared examples for you.

    As I said earlier, QLabel is not a way to go, because You really don't know haw many images user loads, and writing Your own code is, in my opinion, reinventing the wheel, and You are using qt because You don't want to do that, so why not to use a proper qt class to do the job for You?
    Here qLabImg_example..zip is an example that change layout to horizontal, code is buggy and gave an error but ... it's a draft and you will get general idea.
    And here qStdModelExample&#46.zip is what You, in my opinion, should do, using QStandardModel and listView.


    Best luck

  6. The following user says thank you to Talei for this useful post:

    d@nyal (29th April 2010)

  7. #5
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in drwing several images on screen

    Thanks for answer!

    Quote Originally Posted by Talei View Post
    I guess that is a homework, and I saw that You are student of electrical engineering, so due to sentiment of the old days at uni (same department ), I prepared examples for you.

    As I said earlier, QLabel is not a way to go, because You really don't know haw many images user loads, and writing Your own code is, in my opinion, reinventing the wheel, and You are using qt because You don't want to do that, so why not to use a proper qt class to do the job for You?
    Here qLabImg_example..zip is an example that change layout to horizontal, code is buggy and gave an error but ... it's a draft and you will get general idea.
    And here qStdModelExample&#46.zip is what You, in my opinion, should do, using QStandardModel and listView.

    Best luck
    What I'm going to do is to visualize several digital gates(AND, OR, XOR, .... ) based on input of the program.
    so I need to plot:
    1- several gates(pictures) in different levels.
    2- wires that connect gates.


    now I'm not sure about 2 things with "QStandardModel":
    1. possibility of drawing lines on the QListView. (I tried to draw some lines on your example, but I failed! )
    2. location of each gate. I mean is it possible to adjust position of each gate(and their distances) in plan?

    Thanks in advance,
    -------------
    * P.S: Its not a homework. Its part of research project for optimizing digital gate design.

  8. #6
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in drwing several images on screen

    I solved my problem with help of "qLabImg_example..zip" example. (Thanks to "Talei")
    I defined a method for ploting "gates":
    Qt Code:
    1. void MainWindow::darwAGate(int x,int y,int gate_code)
    2. {
    3. //load images:
    4. QPixmap *pixmap_nand = new QPixmap("../pix/NAND.png");
    5.  
    6. QHBoxLayout *h_layout = new QHBoxLayout();
    7. QLabel *label_tmp = new QLabel(this);
    8. label_tmp->setGeometry(x,y,gate_width_pixel,gate_height_pixel);
    9. label_tmp->setPixmap(*pixmap_nand);
    10. h_layout->addWidget( label_tmp );
    11. this->setLayout(h_layout);
    12. }
    To copy to clipboard, switch view to plain text mode 

    I thinks its possible to plot several pics by the use of dynamic memory allocation("delete" and new) if I don't want to define a separate function/method.... but I didn't try that ...


  9. #7
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem in drwing several images on screen

    Hello,
    I checked yesterday before sleep but didn't reply due to fatigue.
    So...

    QListView allows You not only to display images the way I showed in example, it can also display grid, between each "image". Think of it as n*m matrix, or spreadsheet, it's not exactly that way, but my point here is that You can span grids. And that way you can cheat displaying connection between in/out gates. But that would require a lot of tweaking and is probably not the way to go. (Change listView to tableView in example and set same model to see the difference, maybe table is what You want after all ).
    Alternatively You could subclass one of the widget, that looks closest to Your desired widget that "holds" all the gates, then reimplement paint function, and paint image yourself. Prepare png for gates keeping in mind that in/out for each gates should be in the same position for each gate type. Then simply copy png onto screen.

    But the problem is connection between gates. So what You could do is to implement A Star (A*) algorithm (path finding) and firstly copy all gates onto screen (for A* mark this area as blocked, in row manner ), then mark output of the gate as START for A*, and proper input of another gate as END for A*. How to find position of out/in of gates? As stated before prepare accordingly png so those place will be at fixed position + png offset. Lines could be painted on anothe QPixmap so if there is a need to modification You could implement that as well (simply deleting old connection grid, and repainting new one). That way You could draw a really complex gates, not only simple one. Again that is probably not what you want due to complexity, and I assume that you have predefined png for connections as well.

    Then you could use QGraphicsView, see /qt/examples/graphicsview, especially qt/examples/graphicsview/diagramscene, for examples. I didn't do much work with this, so I can't point you any useful examples that You could use, but last example should give You general idea how to paint these pngs.

    Also I assume that program only draw gates, and don't allow any modification/correction, so, as stated before You could pull it off with QListView but I think easiest way would be going with QGraphicsView, because that allow You scrolling, zooming, etc...

    Best luck with you project
    EDIT: I forget about QPainter, that can draw shapes, lines, etc.. So you could place in table at col 0 row 1-2 gate png, at col 1 row 1-2 span and draw connections, and at col 2 row 1-2 gates pngs.
    Last edited by Talei; 30th April 2010 at 08:04.

  10. #8
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in drwing several images on screen

    Hello,
    I checked yesterday before sleep but didn't reply due to fatigue.
    So...

    QListView allows You not only to display images the way I showed in example, it can also display grid, between each "image". Think of it as n*m matrix, or spreadsheet, it's not exactly that way, but my point here is that You can span grids. And that way you can cheat displaying connection between in/out gates. But that would require a lot of tweaking and is probably not the way to go. (Change listView to tableView in example and set same model to see the difference, maybe table is what You want after all ).
    Alternatively You could subclass one of the widget, that looks closest to Your desired widget that "holds" all the gates, then reimplement paint function, and paint image yourself. Prepare png for gates keeping in mind that in/out for each gates should be in the same position for each gate type. Then simply copy png onto screen.

    But the problem is connection between gates. So what You could do is to implement A Star (A*) algorithm (path finding) and firstly copy all gates onto screen (for A* mark this area as blocked, in row manner ), then mark output of the gate as START for A*, and proper input of another gate as END for A*. How to find position of out/in of gates? As stated before prepare accordingly png so those place will be at fixed position + png offset. Lines could be painted on anothe QPixmap so if there is a need to modification You could implement that as well (simply deleting old connection grid, and repainting new one). That way You could draw a really complex gates, not only simple one. Again that is probably not what you want due to complexity, and I assume that you have predefined png for connections as well.

    Then you could use QGraphicsView, see /qt/examples/graphicsview, especially qt/examples/graphicsview/diagramscene, for examples. I didn't do much work with this, so I can't point you any useful examples that You could use, but last example should give You general idea how to paint these pngs.

    Also I assume that program only draw gates, and don't allow any modification/correction, so, as stated before You could pull it off with QListView but I think easiest way would be going with QGraphicsView, because that allow You scrolling, zooming, etc...

    Best luck with you project
    EDIT: I forget about QPainter, that can draw shapes, lines, etc.. So you could place in table at col 0 row 1-2 gate png, at col 1 row 1-2 span and draw connections, and at col 2 row 1-2 gates pngs.
    Alright thanks Telei !

Similar Threads

  1. Problem with Splash Screen ?
    By vinod in forum Qt Programming
    Replies: 13
    Last Post: 11th April 2020, 17:15
  2. Touch screen problem in Qtopia
    By geetha in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 12th September 2008, 07:33
  3. On Screen Keyboard Problem
    By cutie.monkey in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2008, 13:28
  4. Available Screen Problem
    By December in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2007, 16:01
  5. Problem with screen update...
    By mysearch05 in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2006, 18:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.