Solved how to move a draw (Qpainter) to Qlabel ?
-
@mrjj i add this function outside the painEvent :
void thirdwindow::drawlabel(){
int h = ui->label_pic->height();
int w = ui->label_pic->width();QPixmap pix(w, h); // give pixmap some size QPainter painter(&pix); // assign painter to it. note the & pix.fill( Qt::white );
}
==> nothing has changed (I get the first board ) but when I add it to the constructor , I get totally the oposite of what I want :
==> This is why I want to draw with pixmap (to use setPixmap after in a specific label)
-
Hi
Im not sure i know what you are trying.
Are you showing all your code as
drawlabel does nothing with the pixmap ??So what are you trying ?
You have thirdwindow with paint event that draws the board.Then you have drawlabel that should to what ?
-
yes,actually the drawlabel did nothing to the code so i add the code to the constructor of this window but i get the oposite of what i want, here is the code:
thirdwindow::thirdwindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::thirdwindow)
{
ui->setupUi(this);
int h = ui->label_pic->height();
int w = ui->label_pic->width();QPixmap pix(w, h); // give pixmap some size QPainter painter(&pix); // assign painter to it. note the & pix.fill(Qt::white); ui->label_pic->setPixmap(pix);
}
-
@mayyy
hi
But that gives you exactly what picture shows.
A big white rectangle the size of the label.What did you expect it to show ?
-
@mrjj instead of a white rectangle I want the chess board and the rest is empty (to add some extensions for the game )
-
@mayyy
Ahh. well you can still do that but you need the code from paint Event in drawLabel
then and remove code in paintevent so it dont draw in background.void thirdwindow::makeBoard() { // Initialization int h = ui->label_pic->height(); int w = ui->label_pic->width(); QPixmap pix(w, h); // give pixmap some size QPainter painter(&pix); // assign painter to it. note the & 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 the background. The whole widget is drawed. //painter.setBrush(QColor(0,0,0)); //black pix.fill( Qt::black); painter.drawRect(wRect); // Draw the cells which are of the other color // Note: the grid may not fit in the whole rect, because the size of the widget // should be a multiple of the size of the cells. If not, a black line at the right // and at the bottom may appear. //painter.setBrush(QColor(255,255,255)); pix.fill( Qt::white );//white for (unsigned int j = 0; j < numCellY; j++) for (unsigned int i = j % 2; i < numCellX; i += 2) painter.drawRect(i * cellSizeX, j * cellSizeY, cellSizeX, cellSizeY); ui->label_pic->setPixmap(pix); // set to label }
and in constructor just call
makeBoard(); -
@mrjj thank u but , this is what I get : the board is not drawn correctly
-
Hi
Just small bug
we say
QRect wRect = rect(); but that gives us rect of the window
so we need to say
QRect wRect = ui->label_pic-rect();so we use the labels rect for calculations.
-
@mrjj This one is the closet to the goal ! ,but there is no black rect()
-
@mayyy
You call pix.fill and not
painter.setBrushvoid MainWindow::makeBoard() { // Initialization int h = ui->label_pic->height(); int w = ui->label_pic->width(); QPixmap pix(w, h); // give pixmap some size QPainter painter(&pix); // assign painter to it. note the & unsigned int numCellX = 8, numCellY = 8; QRect wRect = ui->label_pic->rect(); unsigned int cellSizeX = wRect.width() / numCellX; unsigned int cellSizeY = wRect.height() / numCellY; //QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); // Draw the background. The whole widget is drawed. //painter.setBrush(QColor(0,0,0)); //black pix.fill( Qt::black); painter.drawRect(wRect); // Draw the cells which are of the other color // Note: the grid may not fit in the whole rect, because the size of the widget // should be a multiple of the size of the cells. If not, a black line at the right // and at the bottom may appear. painter.setBrush(QColor(255,255,255)); // CHANGED // pix.fill( Qt::white );//white THIS IS WRONG for (unsigned int j = 0; j < numCellY; j++) for (unsigned int i = j % 2; i < numCellX; i += 2) painter.drawRect(i * cellSizeX, j * cellSizeY, cellSizeX, cellSizeY); ui->label_pic->setPixmap(pix); }
-
@mrjj This is perfect ! thank u so much for ur help.