How to check which QListWidget has selected item

1.4k views Asked by At

i'm trying to do an application just drawing couple shapes and then if selected from one of three QWidgetLists and clicked a button, the selected shape would turn red. Drawing etc. isn't a problem, but i can't find out how can i check which list is active and has selected item. Current code looks like this:

    QPixmap pixmap(ui->display_field->width(),ui->display_field->height());
    pixmap.fill("transparent");
    int chosen_one;

    if(ui->radio_circle->isChecked()){
        if(circles_list.count() > 0){
            chosen_one = ui->circles_list_wgt->currentItem()->text().toInt();
            circles_list[chosen_one].setColor(Qt::red);
            for(int i=0; i<circles_list.count(); i++) circles_list[i].draw(&pixmap);
            circles_list[chosen_one].setColor(Qt::black);
        }

        for(int i=0; i<rectangles_list.count(); i++) rectangles_list[i].draw(&pixmap);
        for(int i=0; i<triangles_list.count(); i++) triangles_list[i].draw(&pixmap);
    }

    if(ui->radio_rect->isChecked()){
        if(rectangles_list.count() > 0){
            chosen_one = ui->rectangles_list_wgt->currentItem()->text().toInt();
            rectangles_list[chosen_one].setColor(Qt::red);
            for(int i=0; i<rectangles_list.count(); i++) rectangles_list[i].draw(&pixmap);
            rectangles_list[chosen_one].setColor(Qt::black);
        }

        for(int i=0; i<circles_list.count(); i++) circles_list[i].draw(&pixmap);
        for(int i=0; i<triangles_list.count(); i++) triangles_list[i].draw(&pixmap);
    }

    if(ui->radio_tri->isChecked()){
        if(triangles_list.count() > 0){
            chosen_one = ui->triangles_list_wgt->currentItem()->text().toInt();
            triangles_list[chosen_one].setColor(Qt::red);
            for(int i=0; i<triangles_list.count(); i++) triangles_list[i].draw(&pixmap);
            triangles_list[chosen_one].setColor(Qt::black);
        }

        for(int i=0; i<circles_list.count(); i++) circles_list[i].draw(&pixmap);
        for(int i=0; i<rectangles_list.count(); i++) rectangles_list[i].draw(&pixmap);
    }

    ui->display_field->setPixmap(pixmap);

The original app had a bit different method of working, depending on radiobuttons as it is right now. I want it to be item selection dependent only.

1

There are 1 answers

0
Kamajii On

Two issues with your solution:

  1. You actually have three selections: Every QListWidget has its own selection, thus has its own current item.
  2. You should do the drawing in a paintEvent.

I'd suggest the following:

  • Subclass a QWidget that maintains the lists of items you want to draw.
  • Implement the widget's QWidget::paintEvent method. This method is automatically called by Qt when then widget needs to be drawn on the screen. You can manually request this by calling QWidget::update, e.g. when your shape selection has changed.
  • Maybe you even want to factor out the drawing into separate classes, like a Rectangle, a Circle and a Triangle class.

Then create your form including the three QListWidget. Create a single slot and connect the lists' QListWidget::currentRowChanged signals altogether to this single slot. It will hence be called whenever you pick another shape in any of the lists. Inside the slot, you can distinguish using the sender() routine from which of the lists the user picked a shape. Update your drawing widget accordingly, call update and you're done.