Results 1 to 2 of 2

Thread: Layouts, constructors, QPainter - my bigger project in C++ and Qt

  1. #1
    Join Date
    Jul 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Layouts, constructors, QPainter - my bigger project in C++ and Qt

    Hi,
    I am trying to understand layout systems and QPainter class in better way so I am trying to write my own bigge rproject - fractal generator. In this thread I am going to updating my problems with this project so at the beginning i have some.
    Here we have a code:

    juliaset.cpp

    Qt Code:
    1. #include "juliaset.h"
    2.  
    3. JuliaSet::JuliaSet(QWidget *parent) :
    4. QWidget(parent),nMax(30), rMax(2.0),W(600),H(600)
    5. {
    6. this -> setFixedSize(600,600);
    7. this -> setStyleSheet("background-color:yellow;");
    8.  
    9. c = complex<double> (-0.61, 0.4);
    10. }
    11. //*****************************************************
    12. void JuliaSet::paintEvent(QPaintEvent *event){
    13.  
    14. painter = new QPainter(this);
    15. painter -> setPen(QColor(255,255,0) );
    16.  
    17. unsigned short int xe,ye;
    18.  
    19. for(x0=-1.5;x0<=1.5;x0+=0.01)
    20. {
    21. for(y0=-1.5;y0<=1.5;y0+=0.01)
    22. {
    23. z = complex<float>(x0,y0); //change cordinates x and y in complex number
    24. for(int k=1;k<=nMax;k++) //iteration
    25. {
    26. z = pow(z,2)+c;
    27. if(abs(z)>rMax) //disnatnce longer than 2 then stop
    28. break;
    29. else if(abs(z)<=rMax && k ==nMax) //last iteration and abs(z)<2 then draw pixel
    30. {
    31. xe = 200*x0 + 300; //screen coordinates
    32. ye = 200*y0 + 300;
    33. painter -> drawPoint(xe,ye);
    34. }
    35. }
    36. }
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. this -> setStyleSheet("background-color: rgb(35, 5, 5);");
    7.  
    8. unsigned short pos = 0; //help variable for use vector of QLabels and vector of spin boxes
    9.  
    10. this -> setWindowTitle("Fractal Generator v. 1.0");
    11.  
    12. QGridLayout *grid = new QGridLayout(this);
    13. grid->setSpacing(5);
    14.  
    15. for(int j=0; j<3; j++){ //columns
    16. ptrSpinBox = new QSpinBox();
    17. ptrSpinBox -> setFixedSize(60,20);
    18. ptrSpinBox ->setRange(0, 255); //r,g,b value [0;255]
    19. vctrSpinBoxes.push_back(ptrSpinBox);
    20. ptrSpinBox = NULL; //creating spin box and saving into vector
    21.  
    22. if(j%3 == 0) //first spin box is R, second one is G third one is B
    23. ptrQLabelRGB = new QLabel("R");
    24. else if(j%3 == 1)
    25. ptrQLabelRGB = new QLabel("G");
    26. else if(j%3 == 2)
    27. ptrQLabelRGB = new QLabel("B");
    28.  
    29. ptrQLabelRGB ->setFixedSize(10,20);
    30. vctrLabels.push_back(ptrQLabelRGB);
    31. ptrQLabelRGB = NULL; //creating comment R,G,B of spin box and saving into vector
    32.  
    33. grid -> addWidget(vctrLabels[pos], 5,2*j);
    34. grid -> addWidget(vctrSpinBoxes[pos], 5, 2*j+1);
    35. pos++;
    36. }
    37.  
    38. spacer = new QSpacerItem(100, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
    39. grid -> addItem(spacer,5,6);
    40.  
    41. ptrFractalWindow = new JuliaSet();
    42. grid -> addWidget(ptrFractalWindow, 0, 0,5,20);
    43.  
    44. for(int j=7; j<10; j++){ //columns
    45. ptrSpinBox = new QSpinBox();
    46. ptrSpinBox -> setFixedSize(60,20);
    47. ptrSpinBox ->setRange(0, 255); //r,g,b value [0;255]
    48. vctrSpinBoxes.push_back(ptrSpinBox);
    49. ptrSpinBox = NULL; //creating spin box and saving into vector
    50.  
    51. if(j%3 == 1) //first spin box is R, second one is G third one is B
    52. ptrQLabelRGB = new QLabel("R");
    53. else if(j%3 == 2)
    54. ptrQLabelRGB = new QLabel("G");
    55. else if(j%3 == 0)
    56. ptrQLabelRGB = new QLabel("B");
    57.  
    58. ptrQLabelRGB ->setFixedSize(10,20);
    59. vctrLabels.push_back(ptrQLabelRGB);
    60. ptrQLabelRGB = NULL; //creating comment R,G,B of spin box and saving into vector
    61.  
    62. grid -> addWidget(vctrLabels[pos], 5,2*j);
    63. grid -> addWidget(vctrSpinBoxes[pos], 5, 2*j+1);
    64. pos++;
    65. }
    66.  
    67. ptrFractalColor = new QLabel("Fractal Color");
    68. ptrFractalColor -> setFixedSize(120,20);
    69. ptrBackgroundColor = new QLabel("Background Color");
    70. ptrBackgroundColor -> setFixedSize(120,20);
    71.  
    72. grid -> addWidget(ptrFractalColor, 7,2,1,3);
    73. grid -> addWidget(ptrBackgroundColor, 7,16,1,3);
    74.  
    75. generateButton = new QPushButton("Generate");
    76. generateButton -> setFixedSize(120,50);
    77.  
    78. grid -> addWidget(generateButton, 8,6,2,5);
    79.  
    80. realSlider = new QSlider(Qt::Horizontal,0);
    81. imagSlider = new QSlider(Qt::Horizontal,0);
    82.  
    83. grid -> addWidget(realSlider, 9,2,1,3);
    84. grid -> addWidget(imagSlider, 9,16,1,3);
    85.  
    86. ptrRealPartC = new QLabel("Real(c)");
    87. ptrImagPartC = new QLabel("Imag(c)");
    88.  
    89. grid -> addWidget(ptrRealPartC, 10,3,1,3);
    90. grid -> addWidget(ptrImagPartC, 10,17,1,3);
    91. }
    92. //*************************************************************************
    93. MainWindow::~MainWindow()
    94. {
    95. for(unsigned short i=0; i<vctrSpinBoxes.size(); i++){ //deleting pointers
    96. delete vctrSpinBoxes[i];
    97. }
    98.  
    99. for(unsigned short i=0; i<vctrLabels.size(); i++){ //deleting pointers
    100. delete vctrLabels[i];
    101. }
    102.  
    103. delete ptrFractalColor;
    104. delete ptrBackgroundColor;
    105. delete grid;
    106. }
    To copy to clipboard, switch view to plain text mode 

    So I've 2 questions for you.
    1) As you can main window is widqet class. Don't worry about setting buttons, spin boxes etc. in my grid layout, I need to fixed it. But there is one thing which makes me crazy - constructor and his size. I was trying to only create object in constructor and define private method createMenu() where I can set them in layout like this:
    Qt Code:
    1. void MainWindow::createMenu(){
    2. grid -> addWidget(generateButton, 8,6,2,5);
    3. }
    To copy to clipboard, switch view to plain text mode 

    But it doesnt work when I've used this method in constructor - my program can't run. How to make it more elegant and set this menu in another function?

    2) The second one is drawing on QWidget. Is it possible to have Widget with QPainter and other classes which will have draw() method? Or my solution is correct and if I have 4 classes for 4 different fractals I need to derived them from QWidget?

    Rafal

  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: Layouts, constructors, QPainter - my bigger project in C++ and Qt

    Quote Originally Posted by Cthulhu View Post
    But it doesnt work when I've used this method in constructor - my program can't run.
    It doesn't work because you never initialize the "grid" member variable of MainWindow.
    When you try to access it in createMenu() you are accessing an uninitialized pointer.

    Quote Originally Posted by Cthulhu View Post
    2) The second one is drawing on QWidget. Is it possible to have Widget with QPainter and other classes which will have draw() method? Or my solution is correct and if I have 4 classes for 4 different fractals I need to derived them from QWidget?
    Not sure what you are asking but you can make one widget subclass that delegates drawing to another class, so if you have different fractal classes and they have a shared interface, the widget could delegate to any object that has that interface.

    Cheers,
    _

Similar Threads

  1. Some weird constructors
    By highnergy in forum Qt Programming
    Replies: 7
    Last Post: 23rd December 2012, 09:52
  2. Question about constructors and inheritance
    By Blackened Justice in forum Newbie
    Replies: 1
    Last Post: 25th February 2012, 08:03
  3. Qt project management - bigger project
    By Peppy in forum Qt Programming
    Replies: 11
    Last Post: 24th December 2010, 14:50
  4. Using template in constructors
    By estanisgeyer in forum General Programming
    Replies: 4
    Last Post: 18th November 2008, 19:41
  5. constructors, new, return
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 29th September 2006, 15:43

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.