i have lineEdit control & 2 Button i.e Enter and Clear
When the User Presses Clear Button for 4 sec or more the text in the lineEdit should be cleared.
I'm not able to capture the pressEvent on the clear button.
Help Needed.
Printable View
i have lineEdit control & 2 Button i.e Enter and Clear
When the User Presses Clear Button for 4 sec or more the text in the lineEdit should be cleared.
I'm not able to capture the pressEvent on the clear button.
Help Needed.
why do you use events? QPushButton has signal clicked. you can try to use in.
Why ever you want to force the user to press a button 4 seconds, simply subclass QPushButton an implement QWidget::mousePressEvent ( QMouseEvent * event ). (and of course QWidget::mouseReleaseEvent ( QMouseEvent * event ) to measure the time)
Lykurg
here is how u do it buddy:
first use setMouseTracking(true) for your button class. start a timer when the mousePressEvent comes. set a timeout for 4 seconds on the timer and on slot for timeout(), u can clear the text
void YourButtonClass::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
{
Timer->start(m_sec);
m_bMouseButtonDown = true;
}
}
void CAutoRepeatButton::mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
{
m_pAutoRepeatTimer->stop();
m_bMouseButtonDown = false;
if (m_pAutoRepeatTimer->interval() == urInterval)
{
urSlot();
}
}
}
this should solve ur problem
thanks to all for quick reply.
hi talk2amulya,
i'm using simple QPushButton,i have one problem, in your code :
Code:
{ if (event->button() == Qt::LeftButton) { Timer->start(m_sec); m_bMouseButtonDown = true; }
how can i replace YourButtonClass with QPushButton?
is it possible?
install event filter for you button.
Code:
... m_pb->installEventFilter(this); ... { if (e->button() == Qt::LeftButton) { Timer->start(m_sec); m_bMouseButtonDown = true; return true; } } }
hi spirit
i have written the code in following manner :
abc.h
class abc:public QDialog,public QObject
{
}
I'm facing the following error:
abc.h:70: error: only constructors take base initializers
I don't know what the problem is ?
Kindly correct me where i'm going wrong
Thanks in Advanced
Qt doesnt support multiply inheritance from QObject, so you have to correct your code like this
after trying
same error is occuring
show whole header file, plz.
Here is the Header File
Code:
#ifndef ABC_H #define ABC_H #include <QDialog> #include <QTimer> #include <QMouseEvent> { Q_OBJECT public: ~abc; public slots: void log_data_refresh(); protected: private slots: ... void check_queue(); private: QTimer *timer; }; #endif
try to change ~abc; to ~abc();
tried but same error.
try this change bool eventFilter(QObject *obj,QEvent *event): to bool eventFilter(QObject *obj,QEvent *event);
this was the real problem thanks spirit.
:)
Wow,
first of all QWidget::setMouseTracking() is not needed. It is just for getting mouse move events which you aren't need. Then to use a QTimer is overkill because of all the signal and slots thing. Better use a simple QTime::currentTime (). Save the time stamp in press function and in the release function you just calculate the difference and if it is greater then 4 seconds emit a signal.
Lykurg
so that means using QTimer is more expensive than using QTime::currentTime()?
Because I have too much time, or better to much work to get around, here is an example:
Code:
#ifndef MYBUTTON_H #define MYBUTTON_H #include <QtGui> { Q_OBJECT public: void setMinimumHoldTime(int msec) { m_minimumHoldTime = msec; } int minimumHoldTime() const { return m_minimumHoldTime; } protected: signals: void holdForMSec(int); private: QTime m_time; int m_minimumHoldTime; }; #endif // MYBUTTON_H
Code:
#include "mybutton.h" {} { } { int msec = m_time.elapsed(); // Do time check in the button class if (m_minimumHoldTime < msec && rect().contains(event->pos())) emit holdForMSec(msec); // or let the reciver decide what to do // if (rect().contains(event->pos())) // emit holdForMSec(msec); }
@talk2amulya: If you call QTimer::start(int) you start a new "event loop" (I don't know how QTimer is exactly implemented), to measure when the time is up and at which end a signal is emitted. And this continuous! Whereas QTime simple returns a time Object. Nothing more...
hi lykurg,
first of all, thanks for investing your time on this..i have one doubt abt what u said..does QTimer really start its own event loop..cuz as far as i've seen, exec() is the only way to start your own event loop..but its always good to know alternative ways to solve a problem..r u sure it starts its own event loop?
Hi,
as I told I don't know exactly if QTimer starts its own thread or the main-gui-thread is used for (what is more likely), but QTimer::start() calls QObject::startTimer() which does nasty things in d->threadData->eventDispatcher.
Whereas QTime::currentTime() simply returns a QDate. So without deeper knowledge you can say QTime::currentTime() isn't so expensive. But to be true, the resource saving in that case isn't so much...
Lykurg
thanks to Lykurg, spirit , talk2amulya for spending their valuable time and helping me to solve the problem.