PDA

View Full Version : Drawing a rectangle over Qlabel image



cheyanne
10th October 2011, 10:55
I have a map as a image in my Qlabel, now i want to draw a rectangle over the image how to i do it? Because at the end of the day i need the rect(which is my robot footprint to be able to move accordingly. i tried :

MainWindow::MainWindow(Tqt_interface* tqt, QWidget *parent ) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix("/home/liqi/Qt_robotino/src/image/ariccmap.png");
ui->labelmap->setPixmap(pix);

QRectF rectangle(10.0, 20.0, 200.0, 200.0);
QPainter painter(this);
painter.drawRect(rectangle); // drawing code
}


but the rectangle does not appear. Any helps is appreciated.

wysota
10th October 2011, 12:37
You are trying to draw on the widget instead of the pixmap.

cheyanne
11th October 2011, 05:40
Then how do I make it to draw on the image instead?

wysota
11th October 2011, 07:57
Open the painter on the image and not on the widget.

joginder11singh
11th October 2011, 10:35
I am new Qt . I tried to draw a line on image using Qpainter on QPixmap
But image did not load just everything was black on Qlabel conataining Qpixmap;
I dont why
my code is

QPixmap pix(500,500);
ui->label->resize(pix.size());
pix.load(QString::fromUtf8("C:\raster.jpg"));
QPainter painter(&pix);
QPen Red((QColor::QColor(255,0,0)),10);
painter.setPen(Red);
painter.drawLine(10,10,50,50);
ui->label->setPixmap(pix);

problem is that raster.jpg image is not loading ....only whole black is appearing

cheyanne
11th October 2011, 12:52
MainWindow::MainWindow(Tqt_interface* tqt, QWidget *parent ) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QImage image("/home/liqi/Qt_robotino/src/image/ariccmap.png");

// tell the painter to draw on the QImage
QPainter* painter = new QPainter(&image); // sorry i forgot the "&"
painter->setPen(Qt::red);
painter->setFont(QFont("Arial", 10));
// you probably want the to draw the text to the rect of the image
painter->drawText(image.rect(), Qt::AlignBottom, "Text on Image");

QLabel* imageLabel = new QLabel();
ui->labelmap->setPixmap(QPixmap::fromImage(image));
}

Ok, now i managed to at least get text over the image, now, how do i go about placing one image over the previous one instead of text?

wysota
11th October 2011, 13:12
Such step by step approach is not a good way of solving problems. QLabel might be fit for displaying a simple pixmap however if you keep increasing complexity like that, you'll reach a state when you'll want to implement a 3d game and QLabel is not the best candidate for it. Maybe you should first say what your goal is and then we'll suggest a solution (I can already suggest using QGraphicsView instead of QLabel).

cheyanne
11th October 2011, 13:20
Okay, im actually trying to display a map on Qt, then insert a "robot footprint' on top of the map. The laptop with Qt design is client side, it will receive information from the server side(another laptop on the robot) and the robot footprint will move accordingly on the map. my server side is able to receive the x,y,z position now.

wysota
11th October 2011, 14:25
Use Graphics View.

cheyanne
11th October 2011, 14:50
Alright, but when i switched to QgraphicsView, my image is not being displayed again. Here is my code:

MainWindow::MainWindow(Tqt_interface* tqt, QWidget *parent ) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QPixmap pixMap("/home/liqi/Qt_robotino/src/image/ariccmap.png");

QGraphicsScene scene;
scene.addPixmap(pixMap);
ui->graphicsViewmap->setScene(&scene);
ui->graphicsViewmap->show();

}

wysota
11th October 2011, 14:54
Your scene object goes out of scope.

cheyanne
11th October 2011, 15:03
Sorry but can you elaborate more on it? I don't really understand this. how do know that my scene object goes out of scope?

wysota
11th October 2011, 15:07
Because you created it on the stack as a local variable. This is plain C++ you know...

cheyanne
11th October 2011, 15:46
It might be plain c++ to you but i dont really study much of it cos all i was thought is those simple cin cout etc. this is the first time dealing with this so im here asking for help. but anyway, thanks for your replies.

wysota
11th October 2011, 16:17
We can't assume here that you know nothing of programming when writing our replies, we have to assume you know how to program and you only have problems with using Qt. You need a decent knowledge of C++ to be able to use Qt efficiently. So far it seemed you didn't have problems with programming as such so it was safe to assume you just didn't notice you were using a local variable or that you didn't know it mattered.

cheyanne
13th October 2011, 04:55
im able to draw over the image now but how do i put it to keep refreshing and change its position ?? I was thinking of putting while loop at first but it will just keep looping the same thing. any idea?

wysota
13th October 2011, 09:05
Use QGraphicsItem::setPos() to modify the position of an item.