2016-12-20 14 views
-1

私はクリックしたときに画像をQLabelに追加しようとしているプロジェクトがあります。クリック可能なQLabelの作成方法に関する記事をいくつか読んだが、画像を追加するのは難しいようだ。それを行う方法は?クリックしたときにQLabelに画像を追加する

EDITの1-

私はQTでシンプルなチックタックつま先のゲームを作っています。だから私は9 QLabelsのグリッドレイアウトを作った。そして、私はそれがクリックされると、対応するQLabel(OまたはX)に画像を追加したいと思います。 2d配列を使って勝者のステータスをチェックしました。 ここでQLabelから継承し、クリックされた信号とより多くの何を発する私もクラスClickableLabelを持ってmainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

//QPixmap imageO("C:/Users/user/Documents/tictactoe/o.png"); 
//QPixmap imageX("C:/Users/user/Documents/tictactoe/x.png"); 
char a[3][3],turn_player='O'; 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    int i,j; 
    ui->setupUi(this); 
    for(i=0;i<3;i++) 
    { 
     for(j=0;j<3;j++) 
      a[i][j]='-'; 
    } 
    centralWidget = new QWidget(this); 
    ui->setupUi(this); 
    windowstart(); 
    this->setCentralWidget(centralWidget); 
    grid = new QGridLayout(centralWidget); 
    for(int i=0;i<3;i++) 
    { 
     for(int j=0;j<3;j++) 
     { 
      labels[i][j]= new ClickableLabel("",this); 
      grid->addWidget(labels[i][j], i+1, j); 
      labels[i][j]->setAlignment(Qt::AlignCenter); 
      looks(i,j); 
     } 
    } 
    play(); 
} 

void MainWindow::windowstart() 
{ 
    setWindowTitle("TIC-TAC-TOE"); 
    setFixedSize(500,500); 
    QPixmap bkgnd("C:/Users/user/Documents/tictactoe/background.jpg"); 
    bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio); 
    QPalette palette; 
    palette.setBrush(QPalette::Background, bkgnd); 
    this->setPalette(palette); 
} 


void MainWindow::looks(int i,int j) 
{ 
     labels[i][j]->setStyleSheet("QLabel { background-color: #000000; color: white; font:20pt; font-weight:500; border-radius: 10px;}"); 
} 

void MainWindow::play() 
{ 
    //int count=0; 
    for(; ;) 
     { 
      move_Player('O'); 
      count++; 
      if(woncheck('O')==true) 
      { 
       QMessageBox::information(this,tr("Game Over"),tr("Player A(O) won.")); 
       break; 
      } 
      if(count==9) 
      { 
       QMessageBox::information(this,tr("Game Over"),tr("Player A(O) won.")); 
       break; 
      } 
      move_Player('X'); 
      count++; 
      if(woncheck('X')==true) 
      { 
       QMessageBox::information(this,tr("Game Over"),tr("Player B(X) won.")); 
       break; 
      } 
     } 
} 
void MainWindow::move_Player(char ch) 
{ 
    if(ch=='O') 
     //set picture of O 
    else 
     //set picture of X 
} 




bool MainWindow::woncheck (char test) 
{ 
    int i,j,counter1,counter2,counter3,counter4; 
     for(i=0;i<3;i+=1) 
     { 
      counter1=0;counter2=0;counter3=0,counter4=0; 
      for(j=0;j<3;j+=1) 
      { 
       if(a[i][j]==test) 
        counter1++; 
       if(a[j][i]==test) 
        counter2++; 
       if(a[j][j]==test) 
        counter3++; 
       if(a[j][4-j]==test) 
        counter4++; 
      } 
      if(counter1==3||counter2==3||counter3==3||counter4==3) 
       return true; 
     } 
     return false; 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

のコードです。 QLabelがClickableLabelクラスにクリックされたときにイメージを追加する関数を追加することも考えましたが、実際にそれを行う方法を理解できませんでした。

+0

である私が使用することを呼び出して、以下の答えましたが、あなたの質問には詳細に短いです。何が難しいようですか? – goug

答えて

1

画像を追加する方法はQLabel::setPixmap.reference

関連する問題