Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sudoku QLineEdit focusInEvent
Qt 6.11 is out! See what's new in the release blog

Sudoku QLineEdit focusInEvent

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 572 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • privatepepperP Offline
    privatepepperP Offline
    privatepepper
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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 ?

      1 Reply Last reply
      0
      • privatepepperP Offline
        privatepepperP Offline
        privatepepper
        wrote on last edited by privatepepper
        #3

        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 );
        
        }
        // ...........
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          privatepepperP 1 Reply Last reply
          0
          • mrjjM mrjj

            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.

            privatepepperP Offline
            privatepepperP Offline
            privatepepper
            wrote on last edited by
            #5

            @mrjj
            I will try it:)

            Thanks for help

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved