Results 1 to 6 of 6

Thread: I can't move my slider. Heelp

  1. #1
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default I can't move my slider. Heelp

    I'm learning qt anc c++ and recently i wrote this using clock example. Everything is ok except i can't move slider using mouse, keyboard arrows works. What's wrong? :

    main.cpp isn't modified


    mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QLabel>
    6. #include <QTimer>
    7. #include <QSlider>
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. QSlider *slider;
    18. QLabel *tekst;
    19. QTimer *timer;
    20. explicit MainWindow(QWidget *parent = 0);
    21. ~MainWindow();
    22. public slots:
    23. void updated();
    24. void paintEvent(QPaintEvent *event);
    25. void predkosc(int a);
    26.  
    27. private:
    28.  
    29. Ui::MainWindow *ui;
    30. };
    31.  
    32. #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. #include <QPainter>
    4. int lol=0;
    5. int v=1;
    6. float ha;
    7. void MainWindow::predkosc(int a){
    8. timer->start(15-a);
    9. }
    10. void MainWindow::updated(){
    11. ha=lol/60.00;
    12. tekst->setText(QString::number(lol/360));
    13. lol++;
    14. if (lol==21600)
    15. lol=0;
    16. update();
    17. }
    18. MainWindow::MainWindow(QWidget *parent) :
    19. QMainWindow(parent),
    20. ui(new Ui::MainWindow)
    21. { this->setFixedSize(500,800);
    22. timer = new QTimer(this);
    23. slider= new QSlider(Qt::Horizontal, this);
    24. slider->setValue(3);
    25. slider->setMaximum (15);
    26. slider->setMinimum (1);
    27. slider->setGeometry(QRect((width()/2)-100, (height()*8/9), 200, 50));
    28. tekst = new QLabel(this);
    29. tekst->setStyleSheet("font: 38pt;");
    30. tekst->setGeometry(QRect((width()/2)-10, (height()*7/9), 80, 80));
    31. connect(timer, SIGNAL(timeout()), this, SLOT(updated()));
    32. connect(slider, SIGNAL(valueChanged(int)), this, SLOT(predkosc(int)));
    33. predkosc(3);
    34. ui->setupUi(this);
    35. }
    36.  
    37. MainWindow::~MainWindow()
    38. {
    39. delete ui;
    40. }
    41.  
    42.  
    43.  
    44. void MainWindow::paintEvent(QPaintEvent *){
    45. QPainter painter(this);
    46. static const QPoint hourHand[3] = {
    47. QPoint(4, 4),
    48. QPoint(-4, 4),
    49. QPoint(0, -40)
    50. };
    51. static const QPoint minuteHand[3] = {
    52. QPoint(4, 4),
    53. QPoint(-4, 4),
    54. QPoint(0, -90)
    55. };
    56.  
    57. QColor hourColor(127, 0, 127);
    58. QColor minuteColor(0, 127, 127, 191);
    59. int side = qMin(width(), height());
    60. painter.setRenderHint(QPainter::Antialiasing);
    61. painter.translate(width() / 2, height() / 2);
    62. painter.scale(side / 200.0, side / 200.0);
    63. painter.setPen(Qt::NoPen);
    64. painter.setBrush(hourColor);
    65. painter.rotate(ha);
    66. painter.drawConvexPolygon(hourHand, 3);
    67. painter.setBrush(minuteColor);
    68. painter.rotate(lol);
    69. painter.drawConvexPolygon(minuteHand, 3);
    70.  
    71. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I can't move my slider. Heelp

    Hi,
    What do you mean by can can't move slider ? are your program freeze ?
    QMainWindow have update() method too. How about you cange your timer slot?

    Best regards,

    myta

  3. #3
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I can't move my slider. Heelp

    Without update() the clock won't move.
    When i'm trying to move the slider using mouse nothing happens, no freeze etc. Like this slider is covered by something invisible therefore unclickable. I can still move the slider using keyboard arrows.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: I can't move my slider. Heelp

    Use layouts to manage your ui instead of manual calls to setGeometry().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    Krystian (10th October 2013)

  6. #5
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I can't move my slider. Heelp

    Hi,
    The problem is you set the "initial mainWindow design" at top of your slider design. Please move location of
    Qt Code:
    1. ui->setupUi(this);
    To copy to clipboard, switch view to plain text mode 
    at top of your constructor mainWindow. So, your constructor is like this :
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. this->setFixedSize(500,800);
    7. timer = new QTimer(this);
    8. slider= new QSlider(Qt::Horizontal, this);
    9. slider->setValue(3);
    10. slider->setMaximum (15);
    11. slider->setMinimum (1);
    12. slider->setGeometry(QRect((width()/2)-100, (height()*8/9), 200, 50));
    13. tekst = new QLabel(this);
    14. tekst->setStyleSheet("font: 38pt;");
    15. tekst->setGeometry(QRect((width()/2)-10, (height()*7/9), 80, 80));
    16. connect(timer, SIGNAL(timeout()), this, SLOT(updated()));
    17. connect(slider, SIGNAL(valueChanged(int)), this, SLOT(predkosc(int)));
    18. predkosc(3);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Best regards,

    Toto

  7. The following user says thank you to myta212 for this useful post:

    Krystian (10th October 2013)

  8. #6
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I can't move my slider. Heelp

    It works, thanks!

Similar Threads

  1. Dynamic label to right of slider resizes slider
    By zenzero-2001 in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2013, 10:11
  2. Replies: 0
    Last Post: 2nd August 2010, 11:17
  3. Problem in Move Move Event Handler.
    By redgoof in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 11:45
  4. Replies: 2
    Last Post: 21st March 2010, 09:01
  5. I hope to move the slider bar in QGraphicsView automatically
    By learning_qt in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2008, 14:35

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
  •  
Qt is a trademark of The Qt Company.