单击QLABEL时,将图像添加到Qlabel中

Adding image to a QLabel when it is clicked

本文关键字:添加 Qlabel 图像 QLABEL 单击      更新时间:2023-10-16

我有一个项目,在单击Qlabel时,我试图将图像添加到Qlabel。我读了一些有关如何制作可单击的Qlabel的文章,但是添加图像似乎很棘手。有什么方法?

编辑1-

我正在QT制作一个简单的TIC TAC TOE游戏。因此,我制作了9个QLABEL的网格布局。我想在单击相应的Qlabel(O或X)中添加图像。我做了一个2D阵列,以检查获胜状态。这是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;
}

我也有一个ClickableLabel,它从Qlabel继承并发出单击信号,仅此而已。我还考虑过将Qlabel单击到ClickableLabel类时添加图像,但无法真正地进行。

添加图像的方法是 QLabel::setPixmap.(参考)