Results 1 to 4 of 4

Thread: QGraphicsView doesn't update on QComboBox SIGNAL

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsView doesn't update on QComboBox SIGNAL

    Hello, I'm having a little problem with a view that I have, I'm using the resizeEvent of the QMainWindow the view is in to call a function that redraws the view with a grid, so far so good, when I resize the window the view resizes along and the grid to. I also have a QComboBox that I use to choose the scale of the grid squares, the currentIndexChanged SIGNAL of the comboBox triggers a slot that calls the function that redraws the grid, but the view only updates after I resize the window again, it was suppose to update the view with the new grid scale as soon as the user changed the comboBox...?

    This is a simplified version of the original that I did to make it easier to see the portion of the program which contains the fault, I tested it and the same behavior shows here...

    main.h:
    Qt Code:
    1. #include <QApplication>
    2. #include "maincontrol.h"
    3.  
    4. int main(int argc,char *argv[])
    5. {
    6. QApplication a(argc,argv);
    7. MainControl m;
    8. m.show();
    9. m.setted=1;
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    maincontrol.h:
    Qt Code:
    1. #ifndef MAINCONTROL_H
    2. #define MAINCONTROL_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsView>
    6. #include <QGraphicsScene>
    7. #include <QComboBox>
    8. #include <QPixmap>
    9. #include <QImage>
    10. #include <QResizeEvent>
    11. #include <QLayout>
    12. #include <QLabel>
    13.  
    14. class MainControl : public QMainWindow
    15. {
    16. Q_OBJECT
    17. public:
    18. explicit MainControl(QWidget *parent = 0);
    19.  
    20. QHBoxLayout *combo_layout;
    21. QHBoxLayout *view_layout;
    22. QVBoxLayout *main_layout;
    23. QWidget *central_widget;
    24.  
    25. QGraphicsView *control_view;
    26. QGraphicsScene *control_scene;
    27. QPixmap *control_pixmap;
    28. QComboBox *control_combobox;
    29. QLabel *debug_label;
    30. int SCALE;
    31. int image_w;
    32. int image_h;
    33. int setted;
    34. int set_pixel_value(int red,int green,int blue);
    35. void process_canvas();
    36. protected:
    37. void resizeEvent(QResizeEvent *e);
    38. signals:
    39.  
    40. public slots:
    41. void update_scale(int s);
    42.  
    43. };
    44.  
    45. #endif // MAINCONTROL_H
    To copy to clipboard, switch view to plain text mode 

    maincontrol.cpp:
    Qt Code:
    1. #include "maincontrol.h"
    2.  
    3. MainControl::MainControl(QWidget *parent) :
    4. QMainWindow(parent)
    5. {
    6. //----------------------initiate properties
    7. SCALE=10;
    8. image_w=300;
    9. image_h=300;
    10. setted=0;
    11. control_scene=new QGraphicsScene;
    12. control_view=new QGraphicsView(control_scene);
    13. control_view->setMinimumSize(image_w,image_h);
    14. control_view->setResizeAnchor(QGraphicsView::NoAnchor);
    15. control_view->setAlignment(Qt::AlignLeft|Qt::AlignTop);
    16.  
    17. control_pixmap=new QPixmap;
    18. control_combobox=new QComboBox;
    19.  
    20. QString c_str;
    21. int cc=1;
    22. while(cc<=10)
    23. {
    24. c_str.setNum(cc);
    25. control_combobox->addItem(c_str);
    26. cc++;
    27. }
    28. control_combobox->setCurrentIndex((SCALE/2)-1);
    29.  
    30. combo_layout=new QHBoxLayout;
    31. view_layout=new QHBoxLayout;
    32. debug_label=new QLabel("");
    33. main_layout=new QVBoxLayout;
    34. central_widget=new QWidget;
    35.  
    36. //----------------------set layouts
    37. combo_layout->addWidget(control_combobox);
    38. view_layout->addWidget(control_view);
    39.  
    40. main_layout->addLayout(combo_layout);
    41. main_layout->addLayout(view_layout);
    42. main_layout->addWidget(debug_label);
    43. central_widget->setLayout(main_layout);
    44.  
    45. this->setCentralWidget(central_widget);
    46.  
    47. //----------------------draw image once
    48. process_canvas();
    49. control_scene->addPixmap(*control_pixmap);
    50. //----------------------connections
    51. connect(control_combobox,SIGNAL(currentIndexChanged(int)),this,SLOT(update_scale(int)));
    52. }
    53. //--------------------------------------- PROCESS CANVAS -----------------------------------------------//
    54. //makes the image AND UPDATES it to the view
    55. //Problem:When resizeEvent calls this function it updates the view as it should according to
    56. //the it's last command (control_view->update()) but when update_scale calls it it doesn't update
    57. //until I resize it again
    58. void MainControl::process_canvas()
    59. {
    60. QImage control_image(image_w,image_h,QImage::Format_RGB32);
    61. control_image.fill(set_pixel_value(0,0,0));
    62. int cy=0;
    63. int cx=0;
    64. while(cy<control_image.height())
    65. {
    66. while(cx<control_image.width())
    67. {
    68. if(cx%(10*SCALE)==0 || cy%(10*SCALE)==0)
    69. {
    70. control_image.setPixel(cx,cy,set_pixel_value(150,150,150));
    71. }
    72. cx++;
    73. }
    74. cx=0;
    75. cy++;
    76. }
    77. control_pixmap->convertFromImage(control_image.scaled(image_w,image_h));
    78. control_view->update();
    79. }
    80. //--------------------------------------- RESIZE EVENT -----------------------------------------------//
    81. //The size of the image was suppose to go along with the view
    82. void MainControl::resizeEvent(QResizeEvent *e)
    83. {
    84. if(setted)
    85. {
    86. int new_w=e->size().width();
    87. int old_w=e->oldSize().width();
    88. int new_h=e->size().width();
    89. int old_h=e->oldSize().width();
    90.  
    91. image_w+=new_w-old_w;
    92. image_h+=new_h-old_h;
    93. process_canvas();
    94. }
    95. }
    96. //--------------------------------------- UPDATE SCALE -----------------------------------------------//
    97. //It is called when the user changes the value of the comboBox
    98. void MainControl::update_scale(int s)
    99. {
    100. SCALE=2*(s+1);
    101. process_canvas();
    102. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView doesn't update on QComboBox SIGNAL

    If you want to update the QGraphicsPixmapItem then you should keep a pointer to that and call setPixmap() with the new pixmap.

    Cheers,
    _

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

    vinnzcrafts (25th February 2014)

  4. #3
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView doesn't update on QComboBox SIGNAL

    It worked, thank you so very much anda_skoa
    Am I the one to mark as SOLVED thread?

  5. #4
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView doesn't update on QComboBox SIGNAL

    Here is what I changed it after skoa's advice:

    In maincontrol.h I declared a QGraphicsPixmapItem pointer to use with the QGraphicsScene:

    Qt Code:
    1. QGraphicsPixmapItem *control_pixmap_item;
    To copy to clipboard, switch view to plain text mode 
    Then in the constructor I used:

    Qt Code:
    1. control_pixmap_item->setPixmap(*control)
    2. control_scene->addItem(control_pixmap_item);
    To copy to clipboard, switch view to plain text mode 

    Then on maincontrol.cpp I changed the update part in the process_canvas function:

    Qt Code:
    1. void MainControl::process_canvas()
    2. {
    3. QImage control_image(image_w,image_h,QImage::Format_RGB32);
    4. control_image.fill(set_pixel_value(0,0,0));
    5. int cy=0;
    6. int cx=0;
    7. while(cy<control_image.height())
    8. {
    9. while(cx<control_image.width())
    10. {
    11. if(cx%(10*SCALE)==0 || cy%(10*SCALE)==0)
    12. {
    13. control_image.setPixel(cx,cy,set_pixel_value(150,150,150));
    14. }
    15. cx++;
    16. }
    17. cx=0;
    18. cy++;
    19. }
    20. control_pixmap->convertFromImage(control_image.scaled(image_w,image_h));
    21. control_pixmap_item->setPixmap(*control_pixmap);
    22. control_view->update();
    23. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. childrenBoundingRect doesn't update
    By sanku in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2011, 11:29
  2. QGLWidget doesn't update
    By qtbnl in forum Qt Programming
    Replies: 12
    Last Post: 14th February 2011, 18:41
  3. QComboBox doesn't update when on hidden widget
    By martinb0820 in forum Qt Programming
    Replies: 1
    Last Post: 10th December 2010, 23:09
  4. QcomboBox update QTreeView
    By josiah47 in forum Newbie
    Replies: 2
    Last Post: 6th March 2009, 00:32
  5. QComboBox does not update
    By Cruz in forum Qt Programming
    Replies: 8
    Last Post: 19th February 2009, 18:56

Tags for this Thread

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.