Results 1 to 6 of 6

Thread: Creating a grid in QTextEdit

  1. #1
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Creating a grid in QTextEdit

    Hello everybody,

    I'm new to QT and coding. What I want to do is a program that "plans a party" (after Rich Gold), where the people walk into the right distances from each other. Those people will get displayed by the first letter of their name (for example: A like Andrew). All other spaces of my grid should be '.'s ^^ I want the grid to be 60*20, so it would look for example like this:
    Qt Code:
    1. ............................................................ //x = 60
    2. ............................................................
    3. ............................................................
    4. ............................................................
    5. ............................................................
    6. ............................................................
    7. ............................................................
    8. ............................................................
    9. ............................................................
    10. ............................................................
    11. ............................................................
    12. ............................................................
    13. ............................................................
    14. ............................................................
    15. ............................................................
    16. ............................................................
    17. ............................................................
    18. ............................................................
    19. ............................................................
    20. ............................................................
    21. //y = 20
    To copy to clipboard, switch view to plain text mode 
    And the final program would look for example like this:
    Qt Code:
    1. ...........A..........................T..................... //x = 60
    2. ................C...........................................
    3. ............................................................
    4. ........................................VW..................
    5. ......................D.....................................
    6. .....B.....................................................
    7. ............................................................
    8. ............................................................
    9. //y = 20
    To copy to clipboard, switch view to plain text mode 

    Edit: What I did already is creating this:

    Qt Code:
    1. QChar grid[20][60];
    2.  
    3. QString tempi ="";
    4. for (int i=0; i<20; i++)
    5.  
    6. {
    7.  
    8. for (int j=0; j<60; j++)
    9.  
    10. {
    11.  
    12. grid[i][j]='.';
    13.  
    14.  
    15. tempi += grid[i][j];
    16.  
    17. txt->setPlainText(tempi);
    18.  
    19. }
    20.  
    21.  
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 
    This way I'm getting all my points displayed. Whoever I still need to add line breaks. After that I need to find a way to change some "coordinates", whoever I already got an idea for that. So the question is, how do I get line breaks into this?
    Last edited by NoRefer; 3rd March 2015 at 12:54.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Creating a grid in QTextEdit

    Using QTextEdit will raise problems in creating paragraphs and avoiding word wraps etc.

    Better use a QLabel, something like this. It is important to select an appropriate font (better be a Raster font)
    Qt Code:
    1. QLabel * txt = new QLabel;
    2. QChar grid[20][60];
    3.  
    4. QString tempi ="";
    5. for (int i=0; i<20; i++)
    6. {
    7. for (int j=0; j<60; j++)
    8. {
    9. grid[i][j]='.';
    10. tempi += grid[i][j];
    11. }
    12. tempi += '\n';
    13. }
    14. txt->setText(tempi);
    15. txt->show();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    NoRefer (3rd March 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating a grid in QTextEdit

    Thanks a lot for your help! You really helped me a lot

    I'm currently trying to figure out how to add a button to my label. I want to initiate each "step" my partyguests will do with it. However I do get this error:
    Qt Code:
    1. C:\Qt\Qt5.3.1\Tools\QtCreator\bin\partyplaner\main.cpp:79: Error: no matching function for call to 'QVBoxLayout::addWidget(QLabel**)'
    2. layout.addWidget(&txt);
    3. ^
    To copy to clipboard, switch view to plain text mode 
    What I tried was this:
    Qt Code:
    1. QLabel *txt = new QLabel;
    2. QPushButton runButton("Run");
    3.  
    4. QChar grid[20][60];
    5.  
    6. QString points ="";
    7. for (int y=0; y<20; y++)
    8. {
    9. for (int x=0; x<60; x++)
    10. {
    11. grid[y][x]='.';
    12. points += grid[y][x];
    13. }
    14. points += '\n';
    15. }
    16. txt->setText(points);
    17. //txt->show();
    18.  
    19.  
    20. QVBoxLayout layout;
    21. layout.addWidget(&txt);
    22. layout.addWidget(&runButton);
    23.  
    24.  
    25. QWidget window;
    26. window.setLayout(&layout);
    27.  
    28. window.show();
    To copy to clipboard, switch view to plain text mode 

    Yet I've not added any function to the button, I will first try to make it change some specific part of the text and later add a function that will then do the steps. I think that's the most useful way to handle it.

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Creating a grid in QTextEdit

    Qt Code:
    1. layout.addWidget(txt); //<<<< remove '&'
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    NoRefer (4th March 2015)

  7. #5
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating a grid in QTextEdit

    Quote Originally Posted by Santosh Reddy View Post
    Qt Code:
    1. layout.addWidget(txt); //<<<< remove '&'
    To copy to clipboard, switch view to plain text mode 
    Thanks a lot, you have been a great help Well of course I continued the project and came quiet far. I will attach a little screenshot from the program so far party.jpg
    Sadly I still got some little problems I can't figure out.

    I'm currently trying to make the "Run" button work. I'm getting this error: cannot call member function 'int structs::getRun()' without object // and setRun()

    structs.h:
    Qt Code:
    1. #ifndef STRUCTS_H
    2. #define STRUCTS_H
    3.  
    4.  
    5.  
    6. class structs
    7. {
    8. public:
    9. structs();
    10. void setRun(int i);
    11. int getRun();
    12.  
    13.  
    14. private:
    15. int isRun = 0;
    16. };
    17.  
    18. #endif // STRUCTS_H
    To copy to clipboard, switch view to plain text mode 
    structs.cpp
    Qt Code:
    1. #include "structs.h"
    2.  
    3. /*struct::structs()
    4. {
    5. }*/
    6.  
    7. void structs::setRun(int i)
    8. {
    9. isRun = i;
    10. }
    11.  
    12. int structs::getRun()
    13. {
    14. return isRun;
    15. }
    To copy to clipboard, switch view to plain text mode 

    I want to use them here:
    Qt Code:
    1. berechnung.cpp (math)
    2. if (structs::getRun() == 1) {
    3. if (i == 7){
    4. if (hypo >= 0){
    5. if (richtung > 22.5 && richtung < 67.5 && _person->positionX < _person->vec1){
    6. _person->positionX = _person->positionX + 1;
    7. qDebug() << "I think I have just done a step to the RIGHT->!!!";
    8. }
    9. else if (richtung > 22.5 && richtung < 67.5 && _person->positionX > _person->vec1){
    10. _person->positionX = _person->positionX - 1;
    11. qDebug() << "I think I have just done a step to the <-LEFT!!!";
    12. }
    13. else if (richtung > 0 && richtung < 22.5 && _person->positionY > _person->vec2){
    14. _person->positionX = _person->positionY - 1;
    15. qDebug() << "I think I have just done a step to the TOP!!!";
    16. }
    17. else if (richtung > 0 && richtung < 22.5 && _person->positionY < _person->vec2){
    18. _person->positionX = _person->positionY + 1;
    19. qDebug() << "I think I have just done a step to the BOTTOM!!!";
    20. }
    21.  
    22. }
    23. richtung = 0;
    24.  
    25. }
    26. structs::setRun(0);
    27. }
    To copy to clipboard, switch view to plain text mode 
    and here:
    Qt Code:
    1. =display.cpp
    2. void display::on_run_clicked()
    3. {
    4. structs::setRun(1);
    5. qDebug() << "Congrats, you managed to click the Bottom: " << structs::getRun();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Any idea how to make this work?

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a grid in QTextEdit

    You are trying to call an instance method like a static method, i.e. using classname::methodname()

    An instance method, as the name suggests, is called on an instance of the class.
    Like setText on the "txt" instance of QLabel, or addLayout() on the "layout" instance of QVBoxLayout in your earlier code snippet.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 4th September 2014, 11:09
  2. Replies: 0
    Last Post: 24th April 2014, 08:54
  3. QTextEdit - margins, indents within QTextEdit
    By Acamapichtli in forum Newbie
    Replies: 12
    Last Post: 9th April 2014, 17:37
  4. Better Grid
    By Buby in forum Newbie
    Replies: 2
    Last Post: 7th September 2010, 09:18
  5. Replies: 1
    Last Post: 16th February 2007, 07:22

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.