Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sudoku QLineEdit focusInEvent



  • Hey,

    I am creating Sudoku game/solver and I am almost done, just couple things left to do - one of them is highlight row and column whenever tile(QLineEdit)
    is selected. So I created a class with focusInEvent

    // focusInEvent header file
     
    #ifndef QLINEEDIT_CLICKABLE_H
    #define QLINEEDIT_CLICKABLE_H
     
    #include <QWidget>
    #include <Qt>
    #include <QLineEdit>
    #include <QVector>
     
    class QLineEdit_Clickable : public QLineEdit
    {
        Q_OBJECT
    public:
        explicit QLineEdit_Clickable(QWidget *parent = nullptr);
     
     
    protected:
     
        void focusInEvent(QFocusEvent *e);
     
    };
     
    #endif // QLINEEDIT_CLICKABLE_H
    
    // focusInEvent source file
     
    #include "qlineedit_clickable.h"
    #include <QMouseEvent>
    #include <QMessageBox>
     
    QLineEdit_Clickable::QLineEdit_Clickable(QWidget *parent)
        : QLineEdit(parent){
     
    }
     
    void QLineEdit_Clickable::focusInEvent(QFocusEvent *e)
    {
        QLineEdit::focusInEvent(e);
     
    }
    

    So my problem is that I don't know how to get my tile(QLineEdit) position, I need them because I need to know which row and column to highlight. Could someone help me how to get them?
    Yeah I know that can I pass row and column to the constructor, and store it in member variables of QLineEdit_Clickable. But then I need to access my tiles (QVector < *QLineEdit>) in QLineEdit_Clickable, and I have no idea how to do that?


  • Lifetime Qt Champion

    Hi and welcome to the forums

    Do you know its row/col when you create QLineEdit_Clickable ?
    or who know it ?

    Who will highlight the place ?

    Could you not store row/col in QLineEdit_Clickable and then emit signal (with col/row) to
    some class else that does this highlighting ?



  • So I will change my QLineEdit_Clickable constructor to this and I will know my tiles coordinates:

     explicit QLineEdit_Clickable(QWidget *parent = nullptr, int y, int x);
    

    Who will highlight the place ?:
    and my focusInEvent code would be:

    void QLineEdit_Clickable::focusInEvent(QFocusEvent *e)
    {
        QLineEdit::focusInEvent(e);
        for (int i = 0; i < 9; i++){
            tiles[i][x].setStyleSheet(....);
            tiles[y][i].setStyleSheet(....);
       } 
    }
    

    But this wont work , because i need to acces somehow tiles which are in my widget header / source file .

    My widget source file:

    SudokuWidget::SudokuWidget(QWidget * parent)
        : QWidget(parent) {
    
        mapper = new QSignalMapper( this );
    
        QIntValidator *pValidator = new QIntValidator( this );  // user can input only 1 - 9
        pValidator->setRange( 1, 9 ); // 1 - 9
    
        // Sudoku big square
        QGridLayout  *pMainLayout = new QGridLayout( this );
        pMainLayout->setSpacing(0);
        pMainLayout->setAlignment(Qt::AlignCenter);
    
        // 3 x 3 array of smaller squares
        QVector< QGridLayout * > bigGrids;
        for ( int y = 0; y < 3; y++ ){
            for ( int x = 0; x < 3; x++ ){
                QGridLayout *pInnerGrid = new QGridLayout( this );
    
                pInnerGrid->setAlignment(Qt::AlignHCenter);
                pInnerGrid->setSpacing(0);
                pInnerGrid->setMargin(0);
    
                pMainLayout->addLayout( pInnerGrid, y, x );
                bigGrids.push_back( pInnerGrid );
            }
        }
    
    
        tiles.resize(81);
        solve_data.resize(81);
        // adds tiles into smaller squares
        for (int y = 0; y < 9; y++){
            for (int x = 0; x < 9; x++){
                int bigGridIndex = box_position( y, x);
                QGridLayout *pInnerGrid = bigGrids[bigGridIndex];
    
                // square coordinates in the box
                int grid_y = y % 3;
                int grid_x = x % 3;
    
                QLineEdit *tile = new QLineEdit_Clickable( this );
    
                int rowColId = x + y * 10;
                mapper->setMapping( tile, rowColId );
                connect( tile, SIGNAL( textEdited(QString) ), mapper, SLOT( map() ) );
    
                // tile customization
                tile->setValidator(pValidator); // input 1 - 9
                tile->setMaxLength(1);
                tile->setFixedSize(30,30);
                tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-style: solid; border-color: gray gray gray gray; }");
                tile->setAlignment(Qt::AlignCenter);
                tile->setFrame(QFrame::Box);
    
                if (y == 0){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-top: 5px; border-style: solid; border-color: black gray gray gray; }");
                }
    
                if (x == 8){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-right: 5px; border-style: solid; border-color: gray black gray gray; }");
                }
                if (y == 8){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-style: solid; border-color: gray gray black gray; }");
                }
                if (x == 0){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-left: 5px; border-style: solid; border-color: gray gray gray black; }");
                }
                if (x == 0 && y == 0){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-top: 5px; border-left: 5px; border-style: solid; border-color: black gray gray black; }");
                }
                if (x == 8 && y == 0){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-top: 5px; border-right: 5px; border-style: solid; border-color: black black gray gray; }");
                }
                if (x == 8 && y == 8){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black black; }");
                }
                if (x == 0 && y == 8){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-left: 5px; border-style: solid; border-color: gray gray black black; }");
                }
                if (x == 5 || x == 2){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-right: 5px; border-style: solid; border-color: gray black gray gray; }");
                }
                if (y == 2 || y == 5){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-style: solid; border-color: gray gray black gray; }");
                }
                if (x == 2 && y == 8){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black gray; }");
                }
                if (x == 5 && y == 8){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black gray; }");
                }
                if ( ( x == 2 || x == 5 || x == 8 ) && y == 5){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black gray; }");
                }
                if ( ( x == 2 || x == 5 || x == 8 ) && y == 2){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black gray; }");
                }
                if (x == 0 && (y == 2 || y == 5)){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-left: 5px; border-style: solid; border-color: gray gray black black; }");
                }
                if ( y == 0 && ( x == 2 || x == 5 ) ){
                    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-top: 5px; border-right: 5px; border-style: solid; border-color: black black gray gray; }");
                }
    
    
    
                pInnerGrid->addWidget( tile, grid_y, grid_x);
    
                // inedex of tile in tiles array
                int tileIndex = y * 9 + x;
                tiles [tileIndex] = tile;
            }
        }
    
        connect( mapper, SIGNAL( mapped( int ) ), this, SLOT( onMapped( int ) ) );
    
    
        // Generate random digits with solvable solution ( 10 digs )
        random_position(10, random_pos_value);
        for (int i = 0;i < 10; i++){
            tiles[random_pos_value[i].first]->setText(QString::number(random_pos_value[i].second));
            tiles[random_pos_value[i].first]->setReadOnly(true);
        }
    
        // solve button
        Solve = new QPushButton( "Solve", this);
        connect( Solve, SIGNAL(clicked()), this, SLOT(on_Solve_clicked()) );
        pMainLayout->addWidget(Solve, 5, 0);
    
        // clear board button
        Solve = new QPushButton( "Generate", this);
        connect( Solve, SIGNAL(clicked()), this, SLOT(on_Reset_clicked()) );
        pMainLayout->addWidget(Solve, 5, 1);
    
        // Finish Button
        Solve = new QPushButton( "Finish", this);
        connect( Solve, SIGNAL(clicked()), this, SLOT(on_Finish_clicked()) );
        pMainLayout->addWidget(Solve, 5, 2);
    
        setLayout( pMainLayout );
    
    }
    // ...........
    

  • Lifetime Qt Champion

    Hi
    Could you not just add a new signal to QLineEdit_Clickable

    signal:
    void HilightMe(int x, int y);

    and in

    void QLineEdit_Clickable::focusInEvent(QFocusEvent *e)
    {
    QLineEdit::focusInEvent(e);
    emit HilightMe ( x,y ) // send the stored x,y

    and then in sudokoWidget have a slot
    that uses this x,y to hilight the right one ?

    and then connect each QLineEdit_Clickable to this slot so it can emit the signal.



  • @mrjj
    I will try it:)

    Thanks for help


Log in to reply