单击QLABEL时,将图像添加到Qlabel中
Adding image to a QLabel when it is clicked
我有一个项目,在单击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.
(参考)
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 单击QLABEL时,将图像添加到Qlabel中
- 使用 QPixmap 和 QLabel 在 Qt Creator 中以编程方式添加图像文件 (.PNG)
- 在 QstandardItemModel 中添加可点击的 Qlabel
- 如何将QLabel添加到QGraphicsItem