Results 1 to 5 of 5

Thread: Sudoku QLineEdit focusInEvent

  1. #1
    Join Date
    Apr 2020
    Location
    Lithuania
    Posts
    24
    Thanks
    17
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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

    Qt Code:
    1. // focusInEvent header file
    2.  
    3. #ifndef QLINEEDIT_CLICKABLE_H
    4. #define QLINEEDIT_CLICKABLE_H
    5.  
    6. #include <QWidget>
    7. #include <Qt>
    8. #include <QLineEdit>
    9. #include <QVector>
    10.  
    11. class QLineEdit_Clickable : public QLineEdit
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit QLineEdit_Clickable(QWidget *parent = nullptr);
    16.  
    17.  
    18. protected:
    19.  
    20. void focusInEvent(QFocusEvent *e);
    21.  
    22. };
    23.  
    24. #endif // QLINEEDIT_CLICKABLE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // focusInEvent source file
    2.  
    3. #include "qlineedit_clickable.h"
    4. #include <QMouseEvent>
    5. #include <QMessageBox>
    6.  
    7. QLineEdit_Clickable::QLineEdit_Clickable(QWidget *parent)
    8. : QLineEdit(parent){
    9.  
    10. }
    11.  
    12. void QLineEdit_Clickable::focusInEvent(QFocusEvent *e)
    13. {
    14. QLineEdit::focusInEvent(e);
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    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 higlight. Could someone help me how to get them?

    Qt Code:
    1. // Sudoku grid widget
    2. #include "sudoku_widget.h"
    3. #include "qlineedit_clickable.h"
    4.  
    5. #include <QGridLayout>
    6. #include <QFrame>
    7. #include <QLineEdit>
    8. #include <QIntValidator>
    9. #include <QMessageBox>
    10. #include <QPushButton>
    11. #include <QRandomGenerator>
    12. #include <QDateTime>
    13. #include <QSet>
    14. #include <iostream>
    15. #include <QSignalMapper>
    16. #include <QMainWindow>
    17. #include <QResizeEvent>
    18. #include <QVBoxLayout>
    19. #include <QLayout>
    20. #include <QFont>
    21. #include <QTimer>
    22.  
    23.  
    24. SudokuWidget::SudokuWidget(QWidget * parent)
    25. : QWidget(parent) {
    26.  
    27. mapper = new QSignalMapper( this );
    28.  
    29. QIntValidator *pValidator = new QIntValidator( this ); // user can input only 1 - 9
    30. pValidator->setRange( 1, 9 ); // 1 - 9
    31.  
    32. // Sudoku big square
    33. QGridLayout *pMainLayout = new QGridLayout( this );
    34. pMainLayout->setSpacing(0);
    35. pMainLayout->setAlignment(Qt::AlignCenter);
    36.  
    37. // 3 x 3 array of smaller squares
    38. QVector< QGridLayout * > bigGrids;
    39. for ( int y = 0; y < 3; y++ ){
    40. for ( int x = 0; x < 3; x++ ){
    41. QGridLayout *pInnerGrid = new QGridLayout( this );
    42.  
    43. pInnerGrid->setAlignment(Qt::AlignHCenter);
    44. pInnerGrid->setSpacing(0);
    45. pInnerGrid->setMargin(0);
    46.  
    47. pMainLayout->addLayout( pInnerGrid, y, x );
    48. bigGrids.push_back( pInnerGrid );
    49. }
    50. }
    51.  
    52.  
    53. tiles.resize(81);
    54. solve_data.resize(81);
    55. // adds tiles into smaller squares
    56. for (int y = 0; y < 9; y++){
    57. for (int x = 0; x < 9; x++){
    58. int bigGridIndex = box_position( y, x);
    59. QGridLayout *pInnerGrid = bigGrids[bigGridIndex];
    60.  
    61. // square coordinates in the box
    62. int grid_y = y % 3;
    63. int grid_x = x % 3;
    64.  
    65. QLineEdit *tile = new QLineEdit_Clickable( this );
    66.  
    67. int rowColId = x + y * 10;
    68. mapper->setMapping( tile, rowColId );
    69. connect( tile, SIGNAL( textEdited(QString) ), mapper, SLOT( map() ) );
    70.  
    71. // tile customization
    72. tile->setValidator(pValidator); // input 1 - 9
    73. tile->setMaxLength(1);
    74. tile->setFixedSize(30,30);
    75. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-style: solid; border-color: gray gray gray gray; }");
    76. tile->setAlignment(Qt::AlignCenter);
    77. tile->setFrame(QFrame::Box);
    78.  
    79. if (y == 0){
    80. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-top: 5px; border-style: solid; border-color: black gray gray gray; }");
    81. }
    82.  
    83. if (x == 8){
    84. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-right: 5px; border-style: solid; border-color: gray black gray gray; }");
    85. }
    86. if (y == 8){
    87. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-style: solid; border-color: gray gray black gray; }");
    88. }
    89. if (x == 0){
    90. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-left: 5px; border-style: solid; border-color: gray gray gray black; }");
    91. }
    92. if (x == 0 && y == 0){
    93. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-top: 5px; border-left: 5px; border-style: solid; border-color: black gray gray black; }");
    94. }
    95. if (x == 8 && y == 0){
    96. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-top: 5px; border-right: 5px; border-style: solid; border-color: black black gray gray; }");
    97. }
    98. if (x == 8 && y == 8){
    99. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black black; }");
    100. }
    101. if (x == 0 && y == 8){
    102. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-left: 5px; border-style: solid; border-color: gray gray black black; }");
    103. }
    104. if (x == 5 || x == 2){
    105. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-right: 5px; border-style: solid; border-color: gray black gray gray; }");
    106. }
    107. if (y == 2 || y == 5){
    108. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-style: solid; border-color: gray gray black gray; }");
    109. }
    110. if (x == 2 && y == 8){
    111. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black gray; }");
    112. }
    113. if (x == 5 && y == 8){
    114. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black gray; }");
    115. }
    116. if ( ( x == 2 || x == 5 || x == 8 ) && y == 5){
    117. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black gray; }");
    118. }
    119. if ( ( x == 2 || x == 5 || x == 8 ) && y == 2){
    120. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-right: 5px; border-style: solid; border-color: gray black black gray; }");
    121. }
    122. if (x == 0 && (y == 2 || y == 5)){
    123. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-bottom: 5px; border-left: 5px; border-style: solid; border-color: gray gray black black; }");
    124. }
    125. if ( y == 0 && ( x == 2 || x == 5 ) ){
    126. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-top: 5px; border-right: 5px; border-style: solid; border-color: black black gray gray; }");
    127. }
    128.  
    129.  
    130.  
    131. pInnerGrid->addWidget( tile, grid_y, grid_x);
    132.  
    133. // inedex of tile in tiles array
    134. int tileIndex = y * 9 + x;
    135. tiles [tileIndex] = tile;
    136. }
    137. }
    138.  
    139. connect( mapper, SIGNAL( mapped( int ) ), this, SLOT( onMapped( int ) ) );
    140.  
    141.  
    142. // Generate random digits with solvable solution ( 10 digs )
    143. random_position(10, random_pos_value);
    144. for (int i = 0;i < 10; i++){
    145. tiles[random_pos_value[i].first]->setText(QString::number(random_pos_value[i].second));
    146. tiles[random_pos_value[i].first]->setReadOnly(true);
    147. }
    148.  
    149. // solve button
    150. Solve = new QPushButton( "Solve", this);
    151. connect( Solve, SIGNAL(clicked()), this, SLOT(on_Solve_clicked()) );
    152. pMainLayout->addWidget(Solve, 5, 0);
    153.  
    154. // clear board button
    155. Solve = new QPushButton( "Generate", this);
    156. connect( Solve, SIGNAL(clicked()), this, SLOT(on_Reset_clicked()) );
    157. pMainLayout->addWidget(Solve, 5, 1);
    158.  
    159. // Finish Button
    160. Solve = new QPushButton( "Finish", this);
    161. connect( Solve, SIGNAL(clicked()), this, SLOT(on_Finish_clicked()) );
    162. pMainLayout->addWidget(Solve, 5, 2);
    163.  
    164. setLayout( pMainLayout );
    165.  
    166. }
    167.  
    168. // ........
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    451
    Thanks
    9
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sudoku QLineEdit focusInEvent

    Hi, the easiest way would be to pass row and column to the constructor, and store it in member variables of QLineEdit_Clickable.

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    laurynas2003 (17th April 2020)

  4. #3
    Join Date
    Apr 2020
    Location
    Lithuania
    Posts
    24
    Thanks
    17
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Sudoku QLineEdit focusInEvent

    But how could I access my tiles (QVector < *QLineEdit>) in QLineEdit_Clickable?

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Sudoku QLineEdit focusInEvent

    But how could I access my tiles (QVector < *QLineEdit>) in QLineEdit_Clickable?
    You don't need to. When your QLineEdit_Clickable class gets a focus in event, have it emit a signal that contains the row and column as arguments. Handle this signal in a slot in SudokuWidget, and let it highlight the rest of the cells in the row and column. It contains the vector that knows about all the other line edits.

    It really isn't necessary to create a new class just to handle the focus in events on the QLineEdits. You could just as easily have installed an event filter on standard QLineEdit and implemented it in SudokuWidget:

    Qt Code:
    1. // SudokuWidget.h
    2.  
    3. class SudokuWidget : public // ...
    4. {
    5.  
    6. protected:
    7. bool eventFilter( QObject * pObj, QEvent * pEvent );
    8. };
    9.  
    10. // SudokuWidget.cpp
    11.  
    12. SudokuWidget:SudokuWidget( QWidget * parent ) //...
    13. {
    14. // ...
    15. tile = new QLineEdit( this );
    16. tile->installEventFilter( this );
    17. // ...
    18. }
    19.  
    20. bool SudokuWidget::eventFilter( QObject * pObj, QEvent * pEvent )
    21. {
    22. if ( pEvent->type() == QEvent::FocusIn )
    23. {
    24. QLineEdit * pEdit = qobject_cast< QLineEdit * >( pObj );
    25. if ( pEdit )
    26. {
    27. // Now look up the line edit in your QVector, determine its row and column, and highlight them
    28. }
    29. }
    30. return QWidget::eventFilter( obj, event ); // Let normal processing continue
    31. }
    To copy to clipboard, switch view to plain text mode 

    See QObject::eventFilter() and QObject:installEventFilter().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    laurynas2003 (17th April 2020)

  7. #5
    Join Date
    Apr 2020
    Location
    Lithuania
    Posts
    24
    Thanks
    17
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Sudoku QLineEdit focusInEvent

    Hey,
    Thanks again, you such an amazing guy, you always help me so much

Similar Threads

  1. Help with my sudoku game
    By laurynas2003 in forum Newbie
    Replies: 2
    Last Post: 7th April 2020, 16:40
  2. Replies: 6
    Last Post: 21st April 2019, 01:28
  3. focusInEvent not call for QLineEdit
    By EarthHobbit in forum Qt Programming
    Replies: 0
    Last Post: 20th May 2015, 23:28
  4. Replies: 5
    Last Post: 10th December 2010, 20:24
  5. QLineEdit and focusInEvent
    By fuzzywuzzy01 in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2007, 00:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.