Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 907725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:35:56+00:00 2026-05-15T16:35:56+00:00

i want to make a simple chess program. So far i’ve made the board

  • 0

i want to make a simple chess program. So far i’ve made the board using QTableWidget and loaded the piece pictures in the cells of table. Now i wnat to use signal and slot so that when user clicks a cell and then click another cell the piece picture from first cell goes to second cell, But I don’t know how to do it.

Note that i don’t want these “piece moves” obey the real chess rules. I only wnat to do the picture replacement between two cells . Later i will make them obey the rules.

here is the code. in this code only table’s item(0,0) has a picture. can anyone say how to write a code so that when i click that item then click to item(1,1) , picture “1.bmp” goes to background of item(1,1)?

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QHBoxLayout>
#include <QTableWidget>
#include <QHeaderView>

class Table : public QWidget
{
    Q_OBJECT

  public:
    Table(QWidget *parent = 0);
slots:
    //??????

};


Table::Table(QWidget *parent)
    : QWidget(parent)
{
  QHBoxLayout *hbox = new QHBoxLayout(this);

  QTableWidget *table = new QTableWidget(8 , 8 , this);

  table->setFixedSize(900,900);
  table->horizontalHeader()->setDefaultSectionSize(100);
  table->verticalHeader()->setDefaultSectionSize(100);

  table->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
  table->verticalHeader()->setResizeMode(QHeaderView::Fixed);

  QString fileName = "/1.bmp";
  QPixmap pic(fileName);

  QBrush brush(pic);

  QTableWidgetItem* item = new QTableWidgetItem();
  item->setBackground(brush);

  table->setItem(0,0,item);

  hbox->addWidget(table);
  setLayout(hbox);
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    Table table;

    table.show();


    return app.exec();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:35:57+00:00Added an answer on May 15, 2026 at 4:35 pm

    There are really two questions here.

    The first one regarding signals/slots with QTableWidgetItem, and the second regarding handling mouse clicks on the QTableWidget.

    Signals Slots on a QTableWidgetItem

    • note: I don’t recommend doing it this way, read all the way to the bottom*

    Using signals and slots requires that the object that emits the signal, have the signal defined in the class definition. Likewise the object that receives a slot, must have that slot declared in the class definition.

    You’ll notice (on the Qt docs) that QTableWidgetItem doesn’t have a signals or slots to set/remove the background brush you are using to draw your picture. So, you will have to subclass QTableWidgetItem, and provide this signals/slots yourself.

    Example:

    class ChessItem : public QTableWidgetItem
    {
        // constructor / destructor
        // other methods
    
    public slots:
        void slotChangeBackground( const QBrush & brush ) 
        {  
            setBackground( brush );
        }
    };
    

    Handling Mouse Clicks on the QTableWidget

    edit: I removed the event handling paragraph, because using QTableWidget’s builtin signals is easier

    QTableWidget offers the cell clicked signal:

    void QTableWidget::cellClicked ( int row, int column )  
    

    So in your Table class add a slot, then connect it the cellClicked signal to it:

    // in your Table's constructor:
    connect( table, SIGNAL( cellClicked(int, int) ), this, SLOT( slotCellClicked(int,int) ) ) );
    // elsewhere...
    void slotCellClicked(int row, int column) {
        // handle mouse clicking here
    }
    

    The problem as I see it is you don’t want to just connect any ol signal to slotChangeBackground, because that would change every background. So I suggest not using signals/slots for changing the background, and instead use QTableWidget::itemAt ( int ax, int ay ) in your slotCellClicked(x,y) to retrieve the item at a coordinate, then call setBackground on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.