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