When the paint event occurs, the update region has normally been erased, so you are painting on the widget's background
btn_terminal->setProperty("SerialObject", QVariant::fromValue(serial));
QPushButton * btn = qobject_cast<QPushButton*>(sender());
auto serial = btn->property("SerialObject").value<SerialPortClass*>();
class MainWindow
{
...
private:
QMap<QButton*, SerialPortClass*> _serialMap;
}
void MainWindow::openTerminal()
{
QPushButton* btn = qobject_cast<QPushButton*>(sender());
auto serial = _serialMap[btn];
auto terminal=new Terminal(serial);
terminal->show();
}
void MainWindow::connectSerialPort()
{
...
_serialMap[btn_terminal] = serial;
}