PDA

View Full Version : I can't move my slider. Heelp



Krystian
8th October 2013, 19:27
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:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QTimer>
#include <QSlider>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
QSlider *slider;
QLabel *tekst;
QTimer *timer;
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void updated();
void paintEvent(QPaintEvent *event);
void predkosc(int a);

private:

Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H



mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
int lol=0;
int v=1;
float ha;
void MainWindow::predkosc(int a){
timer->start(15-a);
}
void MainWindow::updated(){
ha=lol/60.00;
tekst->setText(QString::number(lol/360));
lol++;
if (lol==21600)
lol=0;
update();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{ this->setFixedSize(500,800);
timer = new QTimer(this);
slider= new QSlider(Qt::Horizontal, this);
slider->setValue(3);
slider->setMaximum (15);
slider->setMinimum (1);
slider->setGeometry(QRect((width()/2)-100, (height()*8/9), 200, 50));
tekst = new QLabel(this);
tekst->setStyleSheet("font: 38pt;");
tekst->setGeometry(QRect((width()/2)-10, (height()*7/9), 80, 80));
connect(timer, SIGNAL(timeout()), this, SLOT(updated()));
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(predkosc(int)));
predkosc(3);
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}



void MainWindow::paintEvent(QPaintEvent *){
QPainter painter(this);
static const QPoint hourHand[3] = {
QPoint(4, 4),
QPoint(-4, 4),
QPoint(0, -40)
};
static const QPoint minuteHand[3] = {
QPoint(4, 4),
QPoint(-4, 4),
QPoint(0, -90)
};

QColor hourColor(127, 0, 127);
QColor minuteColor(0, 127, 127, 191);
int side = qMin(width(), height());
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);
painter.setPen(Qt::NoPen);
painter.setBrush(hourColor);
painter.rotate(ha);
painter.drawConvexPolygon(hourHand, 3);
painter.setBrush(minuteColor);
painter.rotate(lol);
painter.drawConvexPolygon(minuteHand, 3);

}

myta212
10th October 2013, 08:24
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

Krystian
10th October 2013, 14:15
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.

wysota
10th October 2013, 14:29
Use layouts to manage your ui instead of manual calls to setGeometry().

myta212
10th October 2013, 16:15
Hi,
The problem is you set the "initial mainWindow design" at top of your slider design. Please move location of

ui->setupUi(this); at top of your constructor mainWindow. So, your constructor is like this :


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setFixedSize(500,800);
timer = new QTimer(this);
slider= new QSlider(Qt::Horizontal, this);
slider->setValue(3);
slider->setMaximum (15);
slider->setMinimum (1);
slider->setGeometry(QRect((width()/2)-100, (height()*8/9), 200, 50));
tekst = new QLabel(this);
tekst->setStyleSheet("font: 38pt;");
tekst->setGeometry(QRect((width()/2)-10, (height()*7/9), 80, 80));
connect(timer, SIGNAL(timeout()), this, SLOT(updated()));
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(predkosc(int)));
predkosc(3);
}


Best regards,

Toto

Krystian
10th October 2013, 17:52
It works, thanks!