Raspbian 2 player chess game
-
The code you posted does not include anything that would bring the QImage on screen.
-
@Quantum1982 said in Raspbian 2 player chess game:
Painter->drawImage(Dest,*Image,Source);
But what about the line :
Painter->drawImage(Dest,*Image,Source); ?
-
That painter does in fact not paint on anything.
Please re-read the class documentation.
-
Ok
I changed it to this :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QImage>
#include <QRectF>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);QRectF target(10.0,10.0,50.0,50.0); QRectF source(0.0,0.0,50.0,50.0); // QImage Image("C:/Users/User/Desktop/WhiteQueen.jpg"); QPixmap Pixmap("C:/Users/User/Desktop/WhiteQueen.jpg"); QPainter Painter; Painter.drawPixmap(target,Pixmap,source);
}
MainWindow::~MainWindow()
{
delete ui;
}Still no chess piece on the screen ?
-
Because you are still using QPainter incorrectly. A bit less than before but still incorrectly.
You should paint what you want on a QPixmap that you will set on a QLabel if you want to show it.
-
Ok, but now I am getting this error
C:\Users\User\Documents\Chess\main.cpp:-1: error: C1041: cannot open program database 'C:\Users\User\Documents\build-Chess-Desktop_Qt_5_12_12_MSVC2015_64bit-Debug\debug\Chess.vc.pdb'; if multiple CL.EXE write to the same .PDB file, please use /FS
-
Ok I fixed it.
Now I am trying this :
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QImage>
#include <QLabel>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);QPixmap Pixmap("C:/Users/User/Desktop/WhiteQueen.jpg"); QLabel WhiteQueen; WhiteQueen.setGeometry(50,50,100,100); WhiteQueen.setPixmap(Pixmap);
}
MainWindow::~MainWindow()
{
delete ui;
}Still nothing appears ?
-
Ok I fixed it.
Now I am trying this :
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QImage>
#include <QLabel>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);QPixmap Pixmap("C:/Users/User/Desktop/WhiteQueen.jpg"); QLabel WhiteQueen; WhiteQueen.setGeometry(50,50,100,100); WhiteQueen.setPixmap(Pixmap);
}
MainWindow::~MainWindow()
{
delete ui;
}Still nothing appears ?
@Quantum1982
QLabel WhiteQueen;
is a local variable. It goes out of scope at the end of the constructor. And you never added theQLabel
to theMainWindow
anyway.I would take some time to look at some basic Qt widgets examples.
-
Ok, so where should I declare the label then for it to be drawn ?
-
Ok, so where should I declare the label then for it to be drawn ?
@Quantum1982
This is not a longterm design plan. But from where you are now:- Move the variable out to be a member of the
MainWindow
class. - Change it to be a pointer.
- Set it to a
new
ed instance back in the constructor, with theMainWindow
as its parent:WhiteQueen = new QLabel(this)
.
At least you should then be able to see it.
I'm not sure I would do this whole chess UI with widgets, probably a graphics scene with
QGraphicsPixmapItem
s instead, but that's a whole different matter. - Move the variable out to be a member of the
-
I am not sure how to do that.
This is what I have so far :
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QImage>
#include <QLabel>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);QString ChessPieces[12]; ChessPieces[0] = "C:/Users/User/Desktop/WhitePawn.jpg"; ChessPieces[1] = "C:/Users/User/Desktop/WhiteRook.jpg"; ChessPieces[2] = "C:/Users/User/Desktop/WhiteKnight.jpg"; ChessPieces[3] = "C:/Users/User/Desktop/WhiteBishop.jpg"; ChessPieces[4] = "C:/Users/User/Desktop/WhiteQueen.jpg"; ChessPieces[5] = "C:/Users/User/Desktop/WhiteKing.jpg"; ChessPieces[6] = "C:/Users/User/Desktop/BlackPawn.jpg"; ChessPieces[7] = "C:/Users/User/Desktop/BlackRook.jpg"; ChessPieces[8] = "C:/Users/User/Desktop/BlackKnight.jpg"; ChessPieces[9] = "C:/Users/User/Desktop/BlackBishop.jpg"; ChessPieces[10] = "C:/Users/User/Desktop/BlackQueen.jpg"; ChessPieces[11] = "C:/Users/User/Desktop/BlackKing.jpg"; QPixmap Pixmap("C:/Users/User/Desktop/WhiteQueen.jpg"); WhiteQueen.setGeometry(50,50,100,100); WhiteQueen.setPixmap(Pixmap);
}
MainWindow::~MainWindow()
{
delete ui;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QLabel>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{
Q_OBJECTpublic:
QLabel WhiteQueen;
MainWindow(QWidget *parent = nullptr);
~MainWindow();private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_HThen there is still no chess piece displayed ?
-
I am not sure how to do that.
This is what I have so far :
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QImage>
#include <QLabel>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);QString ChessPieces[12]; ChessPieces[0] = "C:/Users/User/Desktop/WhitePawn.jpg"; ChessPieces[1] = "C:/Users/User/Desktop/WhiteRook.jpg"; ChessPieces[2] = "C:/Users/User/Desktop/WhiteKnight.jpg"; ChessPieces[3] = "C:/Users/User/Desktop/WhiteBishop.jpg"; ChessPieces[4] = "C:/Users/User/Desktop/WhiteQueen.jpg"; ChessPieces[5] = "C:/Users/User/Desktop/WhiteKing.jpg"; ChessPieces[6] = "C:/Users/User/Desktop/BlackPawn.jpg"; ChessPieces[7] = "C:/Users/User/Desktop/BlackRook.jpg"; ChessPieces[8] = "C:/Users/User/Desktop/BlackKnight.jpg"; ChessPieces[9] = "C:/Users/User/Desktop/BlackBishop.jpg"; ChessPieces[10] = "C:/Users/User/Desktop/BlackQueen.jpg"; ChessPieces[11] = "C:/Users/User/Desktop/BlackKing.jpg"; QPixmap Pixmap("C:/Users/User/Desktop/WhiteQueen.jpg"); WhiteQueen.setGeometry(50,50,100,100); WhiteQueen.setPixmap(Pixmap);
}
MainWindow::~MainWindow()
{
delete ui;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QLabel>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{
Q_OBJECTpublic:
QLabel WhiteQueen;
MainWindow(QWidget *parent = nullptr);
~MainWindow();private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_HThen there is still no chess piece displayed ?
@Quantum1982
You still have not added yourWhiteQueen
onto your main window. I suggested code which will do this. -
I want to add the pieces programmatically. I don't want to drag and drop labels onto the form. These pieces are going to be stored inside an array which will be loaded dynamically.
-
I want to add the pieces programmatically. I don't want to drag and drop labels onto the form. These pieces are going to be stored inside an array which will be loaded dynamically.
@Quantum1982
I know this.I don't know how to explain any better. Drag & drop has never been discussed. I have told you what you need to do, but you have not done it. You really should read enough to understand how widgets work and how you place widgets onto other widgets/forms/windows; at present you do not know just how to put a label on. I suggested earlier that you should "take some time to look at some basic Qt widgets examples", up to you.
-
@Quantum1982
I know this.I don't know how to explain any better. Drag & drop has never been discussed. I have told you what you need to do, but you have not done it. You really should read enough to understand how widgets work and how you place widgets onto other widgets/forms/windows; at present you do not know just how to put a label on. I suggested earlier that you should "take some time to look at some basic Qt widgets examples", up to you.
Ok but the issue is getting the right code sample, not an abstract overview.
-
Ok but the issue is getting the right code sample, not an abstract overview.
@Quantum1982
The code I gave you told you exactly what you need to do:with the
MainWindow
as its parent:WhiteQueen = new QLabel(this)
.You have chosen not to do anything about this, so there's not much point keep telling you. I'll leave you to it.
-
@Quantum1982
The code I gave you told you exactly what you need to do:with the
MainWindow
as its parent:WhiteQueen = new QLabel(this)
.You have chosen not to do anything about this, so there's not much point keep telling you. I'll leave you to it.
Ok but why do you have to use a pointer ?
-
I want to add the pieces programmatically. I don't want to drag and drop labels onto the form. These pieces are going to be stored inside an array which will be loaded dynamically.
@Quantum1982 said in Raspbian 2 player chess game:
These pieces are going to be stored inside an array which will be loaded dynamically.
That's one reason. Another reason is there may be more than 1 white queen. Another reason is that you'll want to create pieces as you go.
If you don't want to use a pointer, set its parent in the header file, or via
setParent()
. Or when you add it properly onto the form, such as ontoMainWindow
'scentralWidget()
, which you are not doing at the moment anyway, so it will appear in an odd place. -
Can you show me exactly where in the code these changes need to be made ?
-
Can you show me exactly where in the code these changes need to be made ?
@Quantum1982
.h
file:public: QLabel *WhiteQueen;
.cpp
file, inMainWindow::MainWindow()
// QLabel WhiteQueen; --- instead of that line the following one: WhiteQueen = new QLabel(this);