Urgent Help needed



  • hello i am trying to implement heaps using Qt my code displays the values of the array and then heaped function heapifys the array to give new values.

    the problem is i need to change color of each rect once swap takes place for example if rect 1 and rect 3 are interchanged they would change to red from yellow then swap values and then change back to normal color

    this is the code of my dialog

    @
    void Dialog::paintEvent(QPaintEvent *e)
    {

    QPainter painter(this);
    QBrush brush(Qt::yellow);
     QBrush brush2(Qt::red);
    QPen pen1(Qt::black);
    pen1.setWidth(6);
    QString str[10];
    
    
    for(int a=0;a<9;a++)
    {
        str[a].append(QString("%1").arg(array[a]));
        QRect rec(((50*a)+50),100,50,50);
        painter.setPen(pen1);
        painter.drawRect(rec);
        painter.fillRect(rec,brush);
        painter.drawText(rec,Qt::AlignCenter,str[a]);
    
    }
    delay();
    heaped();
    //heapify();
    
    QString str2[10];
    
    
    
    for(int a=0;a<9;a++)
    {
        str2[a].append(QString("%1").arg(array[a]));
        QRect rec(((50*a)+50),100,50,50);
        painter.setPen(pen1);
        painter.drawRect(rec);
        painter.fillRect(rec,brush2);
        painter.drawText(rec,Qt::AlignCenter,str2[a]);
    
    
    }
     repaint();
    
    delay();
    

    }

    @

    @void Dialog::heaped()
    {

    while(flag != 0)
    {
        flag = 0;
        for(int i=size-1; i>0; i--)
        {
            int j = i/2;
    
            if(array[j] > array[i])
            {
                QPaintEvent *e;
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
                flag++;
            }
        }
    }
    

    }@

    @Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    flag=1;
    size=9;
    for(int i=0;i<9;i++) // code to set values randomly to the array
    {
    array[i]=(qrand()0)+1;

    }
    ui->setupUi(this);
    

    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    void Dialog::delay() // function to give delay
    {
    QTime time=QTime::currentTime().addSecs(3);
    while(QTime::currentTime()<time)
    {
    QCoreApplication::processEvents(QEventLoop::AllEvents,100);
    }

    }@



  • I made a overridden function that would be called in heaped after the values are swapped so that the two swapped values could be changed to blue .. but wen i run the computer hangs

    @void Dialog::paintEvent2(QPaintEvent *e,int swp1,int swp2)
    {

    QPainter painter(this);
    QBrush brush(Qt::red);
    QPen pen1(Qt::black);
    pen1.setWidth(6);
    QString str[10];
    QBrush brush2(Qt::yellow);
    
    for(int a=0;a<9;a++)
    {
        if(a!=swp1 && a!=swp2)
        {
    
                str[a].append(QString("%1").arg(array[a]));
                QRect rec(((50*a)+50),100,50,50);
                painter.setPen(pen1);
                painter.drawRect(rec);
                painter.fillRect(rec,brush);
                painter.drawText(rec,Qt::AlignCenter,str[a]);
    
        }
        else
        {
            str[a].append(QString("%1").arg((array[a])));
            QRect rec2(50*a+50,100,50,50);
            painter.setPen(pen1);
            painter.drawRect(rec2);
            painter.fillRect(rec2,brush2);
            painter.drawText(rec2,Qt::AlignCenter,str[a]);
    
    
        }
    

    }

    }
    @



  • I think it is not a really good idea to do the complete color processing inside the paintevent.
    Especially the delay methods inside the paint event are wrong.
    You should do the processing outside the paintevent timertriggered in a method in the Dialog class, and control the color of your rectangles with an additional atribute.

    regards
    karl-heinz



  • Also a Qt coding thing is not to use the [] operators. It's more a C thing then Qt and might cause illegal acces issues in memory. Rather use QList <class T> and then the T.at() function. The at function is better protected.
    Greetz


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.