Results 1 to 4 of 4

Thread: Sliders to print a point on a 2D screen

  1. #1
    Join Date
    Apr 2010
    Posts
    17
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Sliders to print a point on a 2D screen

    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:aint(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?
    Last edited by valy12; 13th May 2010 at 18:14.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sliders to print a point on a 2D screen

    Here you go, but you need to reverse the Y slider, but you'll get the "point" :-)

    pointview.h
    Qt Code:
    1. #ifndef POINTVIEW_H
    2. #define POINTVIEW_H
    3.  
    4. #include <QWidget>
    5. #include <QPaintEvent>
    6. #include <QPoint>
    7.  
    8. class PointView : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit PointView(QWidget *parent = 0);
    13.  
    14. void paintEvent(QPaintEvent *event);
    15.  
    16. signals:
    17.  
    18. public slots:
    19. void setX(int x);
    20. void setY(int y);
    21.  
    22. private:
    23. QPoint point;
    24. };
    25.  
    26. #endif // POINTVIEW_H
    To copy to clipboard, switch view to plain text mode 

    pointview.cpp
    Qt Code:
    1. #include "pointview.h"
    2.  
    3. #include <QRect>
    4. #include <QPainter>
    5.  
    6. PointView::PointView(QWidget *parent) :
    7. QWidget(parent)
    8. {
    9. point.setX(0);
    10. point.setY(0);
    11. }
    12.  
    13. void PointView::setX(int x)
    14. {
    15. QRect redrawRect;
    16.  
    17. if (x < point.x())
    18. redrawRect.setRect(x-1, point.y()-1, point.x()+1, point.y()+1);
    19. else
    20. redrawRect.setRect(point.x()-1, point.y()-1, x+1, point.y()+1);
    21.  
    22. point.setX(x);
    23.  
    24. repaint(redrawRect);
    25. }
    26.  
    27. void PointView::setY(int y)
    28. {
    29. QRect redrawRect;
    30.  
    31. if (y < point.y())
    32. redrawRect.setRect(point.x()-1, y-1, point.x()+1, point.y()+1);
    33. else
    34. redrawRect.setRect(point.x()-1, point.y()-1, point.x()+1, y+1);
    35.  
    36. point.setY(y);
    37.  
    38. repaint(redrawRect);
    39. }
    40.  
    41. void PointView::paintEvent(QPaintEvent *event)
    42. {
    43. Q_UNUSED(event)
    44.  
    45. QPainter painter(this);
    46.  
    47. painter.fillRect(rect(), Qt::black);
    48. painter.setPen(Qt::white);
    49. painter.drawPoint(point.x(), point.y());
    50. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "pointview.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow {
    12. Q_OBJECT
    13. public:
    14. MainWindow(QWidget *parent = 0);
    15. ~MainWindow();
    16.  
    17. protected:
    18. void changeEvent(QEvent *e);
    19.  
    20. private:
    21. Ui::MainWindow *ui;
    22.  
    23. PointView *pointView;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QGridLayout>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. ui->horizontalSlider->setRange(1, 100);
    13. ui->verticalSlider->setRange(1,100);
    14.  
    15. QGridLayout *pointlayout = new QGridLayout;
    16. pointView = new PointView(this);
    17. pointlayout->addWidget(pointView);
    18.  
    19. ui->widget->setLayout(pointlayout);
    20.  
    21. connect(ui->horizontalSlider, SIGNAL(sliderMoved(int)), pointView, SLOT(setX(int)));
    22. connect(ui->verticalSlider, SIGNAL(sliderMoved(int)), pointView, SLOT(setY(int)));
    23. }
    24.  
    25. MainWindow::~MainWindow()
    26. {
    27. delete ui;
    28. }
    29.  
    30. void MainWindow::changeEvent(QEvent *e)
    31. {
    32. QMainWindow::changeEvent(e);
    33. switch (e->type()) {
    34. case QEvent::LanguageChange:
    35. ui->retranslateUi(this);
    36. break;
    37. default:
    38. break;
    39. }
    40. }
    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.

  3. The following user says thank you to tbscope for this useful post:

    valy12 (14th May 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sliders to print a point on a 2D screen

    Even better, as I've already called the repaint with a smaller rect:

    Qt Code:
    1. void PointView::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4.  
    5. painter.fillRect(event->rect(), Qt::black);
    6. painter.setPen(Qt::white);
    7. painter.drawPoint(point.x(), point.y());
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Apr 2010
    Posts
    17
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sliders to print a point on a 2D screen

    Cool! exactly what i wanted!
    Thanks!!!

Similar Threads

  1. Print screen key in Qt windows
    By hanumanth in forum Qt Programming
    Replies: 2
    Last Post: 16th April 2010, 15:44
  2. Replies: 1
    Last Post: 3rd December 2009, 14:23
  3. Cancelling Long-running Print/Print Preview
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 16th June 2009, 23:05
  4. display a plot point by point
    By oswalidos in forum Newbie
    Replies: 32
    Last Post: 13th March 2009, 15:37
  5. Point to Pixel Calculation on all screen
    By patrik08 in forum General Programming
    Replies: 0
    Last Post: 3rd April 2008, 08:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.