Decrement QTimer - qt

I have a QTimer and when I click on an object I want to decrement this QTimer. For that, I use the start function which allows, if the timer is launched, to stop it and restart it with the value passed in parameter. My function is called but as long as the timer has not reached a value close to 4100 the time is not decremented. Once the 4100 is reached (around) it works fine, does anyone have an idea why it doesn't work when the remaining time is too high?
testclick.h :
#ifndef TESTCLICK_H
#define TESTCLICK_H
#include <QMouseEvent>
#include <QTimer>
#include <QWidget>
#include <QDebug>
class TestClick : public QWidget
{
private :
QTimer* timer;
public:
TestClick();
~TestClick();
void mousePressEvent (QMouseEvent* event);
};
#endif // TESTCLICK_H
testclick.cpp :
#include "testclick.h"
TestClick::TestClick()
{
timer = new QTimer();
timer->start(8000);
}
TestClick::~TestClick()
{
delete timer;
}
void TestClick::mousePressEvent(QMouseEvent *event)
{
qDebug()<<"before remainingTime :"<<timer->remainingTime();
timer->start(timer->remainingTime() - 200);
qDebug()<<"after remainingTime :"<<timer->remainingTime();
}
main.cpp :
#include "testclick.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestClick test;
test.show();
return a.exec();
}
And I have :
before remainingTime : 5260
after remainingTime : 5260
before remainingTime : 4908
after remainingTime : 4908
before remainingTime : 4780
after remainingTime : 4780
before remainingTime : 4660
after remainingTime : 4660
before remainingTime : 4508
after remainingTime : 4508
before remainingTime : 4348
after remainingTime : 4348
before remainingTime : 4196
after remainingTime : 3996
before remainingTime : 3836
after remainingTime : 3636
before remainingTime : 3476
after remainingTime : 3276
before remainingTime : 3116
after remainingTime : 2916
before remainingTime : 2740
after remainingTime : 2540
before remainingTime : 2380
after remainingTime : 2180
before remainingTime : 2012
I use Qt5 and Debian.

Related

setQuitOnLastWindowClosed(false) has disabled 'x' button on window / dialog QT

In order to get my MessageBox working correctly (and not shutting the whole application) - I have had to setQuitOnLastWindowClosed to False.
This means however that pushing the 'x' button on my QDialog does not exit the application properly. Any help?
Here is the code:
Main.cpp
#include "openingdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
Dialog w;
w.show();
return a.exec();
}
Dialog code:
#include "openingdialog.h"
#include "ui_openingdialog.h"
#include "patientsetup.h"
#include <QDesktopServices>
#include <QUrl>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
connect(ui->guideButton, SIGNAL(clicked(bool)), this, SLOT(guideButtonClicked()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::okButtonClicked()
{
psetup = new pmps_f(this);
psetup->show();
}
void Dialog::guideButtonClicked()
{
QDesktopServices::openUrl(QUrl::fromLocalFile("C:/PMPS/PMPSv1/Instructionsforuse.pdf"));
}
MessageBox code:
void pmps_f::start()
{
QMessageBox msgBox;
msgBox.setText("Pressing OK will start the sedation process with the baseline target effect-site Ce of 0.5mcg/ml");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
int ret = msgBox.exec();
switch(ret) {
case QMessageBox::Ok:
break;
case QMessageBox::Cancel:
break;
}
}
Construction of MainWindow:
pmps_f::pmps_f(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::pmps_f)
{
ui->setupUi(this);
connect(ui->startbutton,SIGNAL(clicked(bool)), this, SLOT(start()));
}

not able to draw line using QGLWiget

New to qt opengl. Am trying to draw a line in my opengl window it is not displaying.
Code is executing without error. Please help me. what is wrong in the below mentioned code.
//glwidget.cpp
#include "glwidget.h"
#include <QDebug>
#include <QGLWidget>
#include <QPainter>
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL()/*initialize*/
{
qDebug()<<"iniialize";
glClearColor(0.0, 0.0, 102.0/255.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void GLWidget::resizeGL(int width, int height)
{
qDebug()<<"resizeGL";
glViewport(0, 0, (GLint)width, (GLint)height);
}
void GLWidget::paintGL()
{
qDebug()<<"paintGL";
glBegin(GL_LINES);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QWidget>
#include <QGLWidget>
#include <QPainter>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
};
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;/*Mainwindow*/
w.show();
return a.exec();
}
Well, you should think about what you told OpengGL to do here:
With your
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
call, you set up your projection matrix to map the world in the area 0 to 1 (in X- and Y-direction) to your screen.
Then you draw a line from 0 to -1 (in X- and Y-direction). That line lies outside your screen now.
Try some better coordinates in your GL_LINES call.
Plus, you might want to start your paintGL() routine with clearing the screen:
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

accessing a ui.label from main.cpp in QT

i am trying to take a feedback data which is published from arduino and subscribed to my GUI created in QT.
Now i have main.cpp and mainwindow.cpp.
main.cpp
#include <QtGui>
#include <ros/ros.h>
#include <QApplication>
#include "../include/abc/main_window.hpp"
#include "std_msgs/String.h"
#include <std_msgs/UInt16.h>
#include <QMainWindow>
#include <std_msgs/Float32.h>
void chatterCallback(const std_msgs::UInt16 &fb_msg){
ROS_INFO("Feedback: [%f]", fb_msg.data);
ui.label_6->setText(QString("%1").arg(fb_msg.data));
}
int main(int argc, char **argv) {
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("feedback",1000, chatterCallback);
ros::spinOnce();
QApplication app(argc, argv);
abc::MainWindow w(argc,argv);
w.show();
w.setWindowTitle("GUI for Controlling Servo Motor");
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
int result = app.exec();
return result;}
mainwindow.cpp
namespace abc {
using namespace Qt;
QSerialPort *serial;
MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
: QMainWindow(parent)
, qnode(argc,argv)
{
ui.setupUi(this);
}
MainWindow::~MainWindow() {}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
ui.label_5->setText(QString("%1").arg(value));
msg.data = ui.label_5->text().toUInt();
ROS_INFO("%d", msg.data);
chatter_pub.publish(msg);
ros::spinOnce();
}
main_window.hpp
#ifndef abc_MAIN_WINDOW_H
#define abc_MAIN_WINDOW_H
#include <QtGui/QMainWindow>
#include "ui_main_window.h"
#include "qnode.hpp"
#include <QtSerialPort/QSerialPort>
#include <ros/ros.h>
#include "std_msgs/UInt16.h"
namespace abc {
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(int argc, char** argv, QWidget *parent = 0);
~MainWindow();
public Q_SLOTS:
private:
Ui::MainWindowDesign ui;
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise <std_msgs::UInt16> ("chatter", 1000);
QSerialPort *arduino;
QNode qnode;
};
} // namespace abc
#endif // abc_MAIN_WINDOW_H
Now when i run this code, it shows me that UI is not declared in main.cpp.
I wanted to display the data from feedback to label_6 (TextBox). The data is only available in main.cpp, any suggestions are highly appreciable.
Thanks in advance.
You will have to pass the fb_msg.data as an argument to the MainWindow constructor. Then you can set the UI element in the MainWindow thread.
Alternatively once you create the MainWindow instance in your main, you can emit a signal which you catch and process in the MainWindow thread.
You cannot modify the UI elements from a different thread.

QT5 mixing QML and opengl draw

i want to mix opengl and QML stuff. I used qt 5 beta 2.
I make a minimalist programm for show you the problem :
main.ccp
#include <QGuiApplication>
#include "back.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Back b;
b.setSource(QUrl("ui.qml"));
b.show();
return a.exec();
}
back.h
#ifndef BACK_H
#define BACK_H
#include <QtQuick>
#include <QtOpenGL>
#include <QTimer>
class Back : public QQuickView
{
Q_OBJECT
public:
~Back();
Back();
public slots :
void paint();
};
#endif // BACK_H
back.ccp
#include "back.h"
Back::Back()
{
setClearBeforeRendering(false);
connect(this, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()), Qt::DirectConnection);
timer->start(3000);
}
Back::~Back()
{
}
void Back::paint()
{
glViewport(0, 0, 150, 150);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 150, 150,0,0,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3ub(255, 0, 0);
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(100, 0);
glVertex2i(100, 100);
glVertex2i(0, 100);
glEnd();
}
ui.qml
import QtQuick 2.0
Item {
width : 150
height : 150
Rectangle {
x : 50
y : 50
width: 100
height: 100
color: "green"
}
}
The first picture is correct :
I have a red square with a green square with offset and a black background.
picture 1
After 3 seconds, the second is uncorrect :
I have only the green square with withe background
picture 2
Is someone know what i make wrong ?

qt specific implementation

I have this snippet of the code:
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
#include <QWidget>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QPushButton *quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));
QLCDNumber *lcd = new QLCDNumber(3);
lcd->setSegmentStyle(QLCDNumber::Flat);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 999);
slider->setValue(0);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(slider, SIGNAL(valueChanged(int)),
lcd, SLOT(display(int)))
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
when I reach the max of the interval, I want to quit, how can I implement this with signals and slots, thanks in advance
Add one more slot-function to your MyWidget class, for example on_maximum_exit(int) like that:
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
public slots:
void on_maximum_exit(int );
};
Define new function:
void MyWidget::on_maximum_exit(int value)
{
if (value == slider->maximum())
close();
}
In MyWidget consctuctor, after connect(slider, SIGNAL(valueChanged(int)),... add:
/* ... */
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(on_maximum_exit(int)));
/* ... */
And of course use Qt docs!
Like I said in the other post this should work for you:
main.cpp
#include <QApplication>
#include "mywidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QObject>
include <QPushButton>
#include <QSlider>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
~MyWidget(){};
public slots:
void getSliderValueAndQuit();
private:
QPushButton *quit;
QSlider *slider;
};
#endif // MYWIDGET_H
myWidget.cpp
#include "mywidget.h"
#include <QWidget>
#include <QObject>
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));
QLCDNumber *lcd = new QLCDNumber(3);
lcd->setSegmentStyle(QLCDNumber::Flat);
slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 999);
slider->setValue(0);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(slider, SIGNAL(valueChanged(int)),lcd, SLOT(display(int)));
connect(slider,SIGNAL(sliderReleased()), SLOT(getSliderValueAndQuit()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
void MyWidget::getSliderValueAndQuit(){
if(slider->value() == slider->maximum())
close();
}

Resources