View Full Version : Sliders to print a point on a 2D screen
valy12
13th May 2010, 17:40
Hello everybody
i'll explain my problem:
i make 2 sliders which give a position in function of 2 axes
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
...
ui->horizontalSlider->setRange(0,431);
ui->verticalSlider->setRange(0,401);
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),ui->lcdNumber, SLOT(display(int)));
connect(ui->verticalSlider, SIGNAL(valueChanged(int)),ui->lcdNumber_2, SLOT(display(int)));
i connect them to LCD to check values.
for the screen, for the moment i just have that in an another file :
Screen2DGL::Screen2DGL(){
background = QBrush(QColor(Qt::black));
}
void Screen2DGL::paint(QPainter *painter, QPaintEvent *event){
painter->fillRect(event->rect(), background);
painter->setPen(Qt::white);
painter->drawPoint(200,200);
}
i would like to print a point with the coordinates gaven by the sliders instead of "200, 200" on a 2D screen...
How can i do?
tbscope
13th May 2010, 20:07
Here you go, but you need to reverse the Y slider, but you'll get the "point" :-)
pointview.h
#ifndef POINTVIEW_H
#define POINTVIEW_H
#include <QWidget>
#include <QPaintEvent>
#include <QPoint>
class PointView : public QWidget
{
Q_OBJECT
public:
explicit PointView(QWidget *parent = 0);
void paintEvent(QPaintEvent *event);
signals:
public slots:
void setX(int x);
void setY(int y);
private:
QPoint point;
};
#endif // POINTVIEW_H
pointview.cpp
#include "pointview.h"
#include <QRect>
#include <QPainter>
PointView::PointView(QWidget *parent) :
QWidget(parent)
{
point.setX(0);
point.setY(0);
}
void PointView::setX(int x)
{
QRect redrawRect;
if (x < point.x())
redrawRect.setRect(x-1, point.y()-1, point.x()+1, point.y()+1);
else
redrawRect.setRect(point.x()-1, point.y()-1, x+1, point.y()+1);
point.setX(x);
repaint(redrawRect);
}
void PointView::setY(int y)
{
QRect redrawRect;
if (y < point.y())
redrawRect.setRect(point.x()-1, y-1, point.x()+1, point.y()+1);
else
redrawRect.setRect(point.x()-1, point.y()-1, point.x()+1, y+1);
point.setY(y);
repaint(redrawRect);
}
void PointView::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.fillRect(rect(), Qt::black);
painter.setPen(Qt::white);
painter.drawPoint(point.x(), point.y());
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "pointview.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
PointView *pointView;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->horizontalSlider->setRange(1, 100);
ui->verticalSlider->setRange(1,100);
QGridLayout *pointlayout = new QGridLayout;
pointView = new PointView(this);
pointlayout->addWidget(pointView);
ui->widget->setLayout(pointlayout);
connect(ui->horizontalSlider, SIGNAL(sliderMoved(int)), pointView, SLOT(setX(int)));
connect(ui->verticalSlider, SIGNAL(sliderMoved(int)), pointView, SLOT(setY(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
Sliders and a widget to contain the pointview are added to the ui file.
But you can improve this.
tbscope
13th May 2010, 20:17
Even better, as I've already called the repaint with a smaller rect:
void PointView::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(event->rect(), Qt::black);
painter.setPen(Qt::white);
painter.drawPoint(point.x(), point.y());
}
valy12
14th May 2010, 13:06
Cool! exactly what i wanted!
Thanks!!!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.