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