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.
Two issues with your solution:
QListWidgethas its own selection, thus has its own current item.paintEvent.I'd suggest the following:
QWidgetthat maintains the lists of items you want to draw.QWidget::paintEventmethod. This method is automatically called by Qt when then widget needs to be drawn on the screen. You can manually request this by callingQWidget::update, e.g. when your shape selection has changed.Rectangle, aCircleand aTriangleclass.Then create your form including the three
QListWidget. Create a single slot and connect the lists'QListWidget::currentRowChangedsignals 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 thesender()routine from which of the lists the user picked a shape. Update your drawing widget accordingly, callupdateand you're done.