用信号和槽QT更新QLineEdit
Update QLineEdit with Signal and Slot QT
我写信是为了征求关于如何最好地使用QT库实现我的代码的建议。我有一个类叫Action类,它每秒钟检索一次PC时间(使用gettimeofday),这个值将显示在GUI中。因此,我有一个类小部件,它定义了GUI所需的所有小部件。该值(以秒为单位)将与QLineEdit一起显示。所以我的问题是,我如何实现信号和插槽来更新QLineEdit中的值?我应该发出一个信号,每次函数retreiveTimetoSend被调用?
action.h
class Action: public object
{
Q_OBJECT
private:
Qtimer timer;
unisgned int timetosend;
private:
void retreiveTimetoSend();
public:
Action();
~Action();
public slots:
void doWork();
}
action.cpp
void retreiveTimetoSend()
{
struct timeval Now;
unsigned int Sec;
gettimeofday(&Now, NULL);
Sec = Now.tv_sec;
time.value =Sec;
}
void Action::Action()
{
timer.setInterval(1000);
connect(&timer, SIGNAL(timeout()), this, SLOT (doWork()));
timer.start();
}
void Action::doWork()
{
retreiveTimetoSend()
}
widget.h
class widgets: public QWidget
{
Q_OBJECT
private:
QLineEdit *displayTime;
public:
widget(action *w_test);
}
widget.cpp
widgets::widgets(action *w_test)
{
displayTime= new QLineEdit();
displayTime->setText(QString::number(w_test->timetosend,10));
displayTC->setStyleSheet("color: blue; background-color: red");
}
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Action *test = new Action;
Thread *threadtest = new QThread;
test->moveToThread(threadtest);
QObject::connect(threadtest, SIGNAL(started()), test ,SLOT(doWork()));
widget *mainwindows = new widget(test);
mywindow->show();
threadtest->start();
return app.exec();
}
使用gettimeofday
使用QTime::currentTime然后将其转换为字符串(选择格式)并发出结果。此信号应连接到插槽QLineEdit::setText。
在这里使用thread是完全过时的
相关文章:
- 从C++本机插件更新Vector3数组
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- cmake更新缓存的变量
- Qt和C++:将QLineEdit添加到QTabWidget中
- 更新到莫哈韦后出现cmath错误
- OpenMP:并行更新数组总是需要减少数组吗
- 如何从QToolBox中动态创建的QLineEdit中获取文本
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 指针没有更新它在void函数内部指向的值
- 如何在c++中获取要更新的值
- 已修改的LinkedList未在文本文件本身中更新
- Qt:当QListView获得新条目时,如何更新QStringList
- 更新的矢量元素不打印
- 如何使用按钮更新GTK3图像以使用C++从相机捕获图片
- 如何在ECS框架中更新组件数据和通知系统
- 从VS 2015更新3更新到VS2015更新3 d后浮点计算行为不同的原因
- 如果我想在没有更新编译器的情况下使用新功能,该怎么办?
- 通过QLineEdit筛选查询并更新QSqlQueryModel
- 在已经包含某些文本的QTextEdit中显示QLineEdit中的文本,并实时更新
- 用信号和槽QT更新QLineEdit