It is very difficult to evaluate the code because it is posted here.
It looks like you created a subclass of QWidget , Form_temp , using Qt Designer, which means that it has extra baggage with a design that it really doesn't need.
When you create your instance of Form_temp , you do not set MainWindow as the parent in the constructor, so I'm not quite sure how your custom widget will draw by itself, since it never gets a show() call. It also never collapsed.
There is no necessary wiring to use the slot and implementation, so it is impossible to determine if this is a problem in this situation.
It should be possible for you to achieve your goal :) I would highly recommend that you look at the Qt Analog Clock Example , as this demonstrates pretty well how to implement your own widget.
You mentioned that you only want your widget to update when data changes, but you donβt understand how the Qt system works. You want your widget to draw itself when you change data, but this is not the only time the widget will draw itself, so you should not try to limit the drawing operation this way.
Put the code in paintEvent() , which will draw the entire widget as you want it to be displayed based on the current data. The frame will execute paintEvent() when the widget first appears, when it is detected after it has previously been closed by another window and in many other situations that you do not control.
Add the usual methods (no need for slots) that will allow you to change the underlying data outside the class and make sure that these methods include the call to update() at the end of them so that they display the structure in which the widget should be repainted.
If your widget is complicated and slow to draw, you can look at the area specified in the event passed to paintEvent() to restrict your drawing code to only the region that needs to be updated.
UPDATE:
Your code is close. I cut it to the ground to demonstrate the basics. You must be able to design it for your needs.
main.cpp
#include <QApplication> #include "MainWindow.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow mainWindow; mainWindow.show(); return app.exec(); }
mainwindow.h
#ifndef _MAINWINDOW_H #define _MAINWINDOW_H #include "Form_temp.h" #include <QWidget> #include <QTimer> class MainWindow : public QWidget { Q_OBJECT public: MainWindow(); virtual ~MainWindow(); private: Form_temp *temp_graph; QTimer m_timer; private slots: void slot_timeout(); }; #endif /* _MAINWINDOW_H */
main.cpp
#include "MainWindow.h" MainWindow::MainWindow(): temp_graph(0), m_timer(this) { temp_graph = new Form_temp(this); connect(&m_timer, SIGNAL(timeout()), this, SLOT(slot_timeout())); m_timer.start(1000); } MainWindow::~MainWindow() { } void MainWindow::slot_timeout() { int y = temp_graph->getY(); y++; if(y > 10) { y = 0; } temp_graph->setY(y); }
Form_temp.h
#ifndef _FORM_TEMP_H #define _FORM_TEMP_H #include <QWidget> class Form_temp : public QWidget { Q_OBJECT public: Form_temp(QWidget *parent = 0); virtual ~Form_temp(); void setY(const int newY); int getY(); protected: void paintEvent(QPaintEvent *event); private: int m_y; }; #endif /* _FORM_TEMP_H */
Form_temp.cpp
#include "Form_temp.h" #include <iostream> #include <QPaintEvent> #include <QPainter> #include <QPen> using namespace std; Form_temp::Form_temp(QWidget *parent) : QWidget(parent), m_y(1) { cout << "Form_temp created." << endl; } void Form_temp::setY(const int newY) { m_y = newY; update(); } int Form_temp::getY() { return m_y; } Form_temp::~Form_temp() { cout << "Form_temp destroyed." << endl; } void Form_temp::paintEvent(QPaintEvent *event) { cout << "Form_temp paintEvent." << endl; QPainter p(this); QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin); p.setPen(pen); p.setRenderHint(QPainter::Antialiasing, true); p.drawLine(0, m_y, width(), m_y); }