can someone help me modify this code. I'm trying to Display a checkerboard C++ QT -


i'm new qt , c++. i'm trying create checker/chess board each square object. i'm trying figure out how have each square object part of board object i'm declaring , display on screen. can display widget on screen using mywidget.show() in main class. want board.show() , have of square objects members of class(that have height, width, , color) show up. code tried nothing showed up, although able square show not in board class.

main.cpp

#include <qtgui> #include "square.h" #include "board.h"  int main(int argc, char *argv[]) {     qapplication app(argc, argv);     //square square;     //square.show();     board board;     board.show();     return app.exec(); } 

board.h , board.cpp

#ifndef board_h #define board_h  #include <qwidget>  class board : public qwidget { public:     board(); };  #endif // board_h  #include "board.h" #include "square.h"  board::board() {     square square;     //square.show(); } 

square.h , square.cpp*strong text*

#ifndef square_h #define square_h  #include <qwidget>  class square : public qwidget { public:     square();  protected:     void paintevent(qpaintevent *); };  #endif // square_h  #include "square.h" #include <qtgui>  square::square() {     qpalette palette(square::palette());     palette.setcolor(backgroundrole(), qt::white);     setpalette(palette); }  void square::paintevent(qpaintevent *) {     qpainter painter(this);     painter.setrenderhint(qpainter::antialiasing);     painter.setbrush(qbrush("#c56c00"));     painter.drawrect(10, 15, 90, 60); } 

your square created automatic variable (ie, lifetime scope of constructor of board). show() make widget visible, long event loop can handle widget, not case here (square deleted before event loop).

also, should add q_object macro in every class derived qobject. board derived qwidget, derived qobject.

so, change class board:

class square; class board : public qwidget { q_object public:     board(); private:     square* square; }; 

the constructor:

board::board() {     square = new square();     square->show(); } 

and destructor:

board::~ board() {     delete square; } 

note: me, having square class useless. can paint grid in paintevent of board, faster, consume less memory, , easier maintain.

edit: here better method:

void board::paintevent(qpaintevent* pe) {     // initialization     unsigned int numcellx = 8, numcelly = 8;     qrect wrect = rect();     unsigned int cellsizex = wrect.width() / numcellx;     unsigned int cellsizey = wrect.height() / numcelly;     qpainter painter(this);     painter.setrenderhint(qpainter::antialiasing);      // draw background. whole widget drawed.     painter.setbrush(qcolor(0,0,0)); //black     painter.drawrect(wrect);      // draw cells of other color     // note: grid may not fit in whole rect, because size of widget     // should multiple of size of cells. if not, black line @ right     // , @ bottom may appear.     painter.setbrush(qcolor(255,255,255)); //white     for(unsigned int j = 0; j < numcelly; j++)         for(unsigned int = j % 2; < numcellx; i+=2)             painter.drawrect(i * cellsizex, j * cellsizey, cellsizex, cellsizey); } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -