Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Urgent Help needed
Forum Updated to NodeBB v4.3 + New Features

Urgent Help needed

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fahd_arshad123
    wrote on last edited by
    #1

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

    }@

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fahd_arshad123
      wrote on last edited by
      #2

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

      }

      }
      @

      1 Reply Last reply
      0
      • K Offline
        K Offline
        khrl01
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          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

          Greetz, Jeroen

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved