// SudokuWidget.h
class SudokuWidget
: public QWidget{
Q_OBJECT;
public:
~SudokuWidget();
private slots:
void onMapped( int rowColId );
private:
};
// SudokuWidget.cpp
SudokuWidget
::Sudokuwidget( QWidget * parent
){
pValidator->setRange( 1, 9 );
// Create grid layout, create 9 frames, create 9 cells in each frame
// ... in the innermost loop where you create the tiles
for ( int row = 0; row < 9; row++ )
{
for ( int col = 0; col < 9; col++ )
{
int rowColId = col + row * 10;
mapper.setMapping( tile, rowColId );
connect( tile, &QLineEdit::editingFinished, mapper, &QSignalMapper::map );
tiles.push_back( tile ); // save the line edit so you can get to it later
// ...
}
}
connect( mapper, SIGNAL( mapped( int ) ), this, SLOT( onMapped( int ) ) );
// ...
}
void SudokuWidget::onMapped( int rowColId )
{
int row = rowColId / 10;
int col = rowColId % 10;
// Retrieve the QString text from the QLineEdit, convert it to an int,
// store it in your game data structure, check to see if it is correct,
// whatever your game decides to do.
}
// SudokuWidget.h
class SudokuWidget : public QWidget
{
Q_OBJECT;
public:
SudokuWidget( QWidget * parent );
~SudokuWidget();
private slots:
void onMapped( int rowColId );
private:
QSignalMapper * mapper;
QVector< QLineEdit * > tiles;
};
// SudokuWidget.cpp
SudokuWidget::Sudokuwidget( QWidget * parent )
: QWidget( parent )
{
mapper = new QSignalMapper( this );
QIntValidator *pValidator = new QIntValidator( this );
pValidator->setRange( 1, 9 );
// Create grid layout, create 9 frames, create 9 cells in each frame
// ... in the innermost loop where you create the tiles
for ( int row = 0; row < 9; row++ )
{
for ( int col = 0; col < 9; col++ )
{
QLineEdit * tile = new QLineEdit( this );
int rowColId = col + row * 10;
mapper.setMapping( tile, rowColId );
connect( tile, &QLineEdit::editingFinished, mapper, &QSignalMapper::map );
tiles.push_back( tile ); // save the line edit so you can get to it later
// ...
}
}
connect( mapper, SIGNAL( mapped( int ) ), this, SLOT( onMapped( int ) ) );
// ...
}
void SudokuWidget::onMapped( int rowColId )
{
int row = rowColId / 10;
int col = rowColId % 10;
QLineEdit * tile = tiles[ row * 9 + col ];
// Retrieve the QString text from the QLineEdit, convert it to an int,
// store it in your game data structure, check to see if it is correct,
// whatever your game decides to do.
}
To copy to clipboard, switch view to plain text mode
Bookmarks