Old image stays when drawing new one
-
i want my program to show a different drawing for every item in the combobox, and it does, but the previous drawing always stays when i choose the next item in the combobox, so eventually there are a lot of drawings, but i only want the one that i selected.
@void MWidget::ChangeFile(int NewIndex)
{
if(NewIndex<airfoils.count())
{
ReadFile(airfoils.at(NewIndex));update(); }
}
//![0]
void MWidget::ReadFile(QString thefile){
m_File_p = new QFile (thefile);if (m_File_p != NULL) { if ( (m_File_p->exists() == true) && (m_File_p->open(QIODevice::ReadOnly | QIODevice::Text) == true) ) { QTextStream stream(m_File_p); QString line = stream.readLine(); bool blnFirstLine =true; float flScale = 640; while (!line.isNull()) { if(blnFirstLine) { blnFirstLine=false; }else{ QStringList coordinates = line.split(",", QString::SkipEmptyParts ); if( coordinates.count() == 2 ) { bool checkX; float x = coordinates.at(0).trimmed().toFloat( &checkX )*flScale+100; bool checkY; float y = coordinates.at(1).trimmed().toFloat( &checkY )*(-1)*flScale+100; if( checkX && checkY ) polyPoints << QPointF(x,y); } } line = stream.readLine(); } } m_File_p->flush(); m_File_p->close(); delete m_File_p; }
}
//![0]
//![1]
MWidget::~MWidget()
{
delete ui;}
//![1]
//![2]
QSize MWidget::sizeHint() const
{
return QSize(840, 480);}
//![2]
//![3]
void MWidget::paintEvent(QPaintEvent * event)
{QWidget::paintEvent(event);
setAttribute(Qt::WA_OpaquePaintEvent);
// i found the QLabel idea online, but it doesnt seem to do anything
QLabel *label = new QLabel(this);
label->setAutoFillBackground(true);QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing,true); QPen linepen(Qt::red); linepen.setCapStyle(Qt::RoundCap); linepen.setWidth(5); painter.setPen(linepen);
painter.drawPolyline(polyPoints2);
update();
}@
-
Something is not right, or your code is not complete:
You stream into "polyPoints", but in paintEvent, you use "polyPoints2".Do you ever clear polyPoints?