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>
{
Q_OBJECT
public:
explicit PointView
(QWidget *parent
= 0);
signals:
public slots:
void setX(int x);
void setY(int y);
private:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
pointview.cpp
#include "pointview.h"
#include <QRect>
#include <QPainter>
PointView
::PointView(QWidget *parent
) :{
point.setX(0);
point.setY(0);
}
void PointView::setX(int x)
{
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)
{
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);
}
{
Q_UNUSED(event)
painter.fillRect(rect(), Qt::black);
painter.setPen(Qt::white);
painter.drawPoint(point.x(), point.y());
}
#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());
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "pointview.h"
namespace Ui {
class MainWindow;
}
Q_OBJECT
public:
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
PointView *pointView;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->horizontalSlider->setRange(1, 100);
ui->verticalSlider->setRange(1,100);
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
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
#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;
}
}
To copy to clipboard, switch view to plain text mode
Sliders and a widget to contain the pointview are added to the ui file.
But you can improve this.
Bookmarks