how to move a draw (Qpainter) to Qlabel ?
-
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
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(); -
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.
-
@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); }