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);
}
}
↧