2016-12-29 26 views
0

私はカップルの図形を描くだけでアプリケーションを実行しようとしています.3つのQWidgetListsの1つから選択してボタンをクリックすると、選択した図形が赤くなります。描画などは問題ではありませんが、どのリストがアクティブで、アイテムを選択しているかを確認する方法がわかりません。現在のコードは次のようになります。どのQListWidgetが選択したアイテムを確認するか

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

オリジナルのアプリは、それが今であるとしてラジオボタンに応じて、作業の少し異なる方法を持っていました。私はそれが項目の選択のみに依存したい。あなたのソリューションと

答えて

0

2つの問題:

  1. あなたが実際に選択を持っている:すべてのQListWidgetは、独自の選択をしていますが、これ自身の現在のアイテムを持っています。
  2. paintEventで図面を作成する必要があります。

私は次のことをお勧めしたい:あなたが描きたいアイテムのリストを維持し

  • サブクラスQWidgetを。
  • ウィジェットのQWidget::paintEventメソッドを実装します。ウィジェットを画面上に描画する必要がある場合、このメソッドはQtによって自動的に呼び出されます。 QWidget::updateに電話することで手動でリクエストできます。あなたの形状選択が変更されたとき。
  • RectangleCircleTriangleクラスのように、図面を別々のクラスに分解したいと思うかもしれません。

次に、3つのQListWidgetを含むフォームを作成します。単一のスロットを作成し、リストのQListWidget::currentRowChanged信号をこの単一のスロットに完全に接続します。したがって、リストの中の他の図形を選択するたびに呼び出されます。スロット内では、sender()ルーチンを使用して、ユーザがシェイプを選択したリストから区別することができます。それに応じて図面ウィジェットを更新し、updateと呼んで完了です。

関連する問題