PDA

View Full Version : Creating a grid in QTextEdit



NoRefer
3rd March 2015, 12:21
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:


.................................................. .......... //x = 60
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
.................................................. ..........
//y = 20

And the final program would look for example like this:


...........A..........................T........... .......... //x = 60
................C................................. ..........
.................................................. ..........
........................................VW........ ..........
......................D........................... ..........
.....B............................................ .........
.................................................. ..........
.................................................. ..........
//y = 20


Edit: What I did already is creating this:



QChar grid[20][60];

QString tempi ="";
for (int i=0; i<20; i++)

{

for (int j=0; j<60; j++)

{

grid[i][j]='.';


tempi += grid[i][j];

txt->setPlainText(tempi);

}



}


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? :)

Santosh Reddy
3rd March 2015, 13:22
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)

QLabel * txt = new QLabel;
QChar grid[20][60];

QString tempi ="";
for (int i=0; i<20; i++)
{
for (int j=0; j<60; j++)
{
grid[i][j]='.';
tempi += grid[i][j];
}
tempi += '\n';
}
txt->setText(tempi);
txt->show();

NoRefer
4th March 2015, 08:51
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:

C:\Qt\Qt5.3.1\Tools\QtCreator\bin\partyplaner\main .cpp:79: Error: no matching function for call to 'QVBoxLayout::addWidget(QLabel**)'
layout.addWidget(&txt);
^
What I tried was this:


QLabel *txt = new QLabel;
QPushButton runButton("Run");

QChar grid[20][60];

QString points ="";
for (int y=0; y<20; y++)
{
for (int x=0; x<60; x++)
{
grid[y][x]='.';
points += grid[y][x];
}
points += '\n';
}
txt->setText(points);
//txt->show();


QVBoxLayout layout;
layout.addWidget(&txt);
layout.addWidget(&runButton);


QWidget window;
window.setLayout(&layout);

window.show();


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.

Santosh Reddy
4th March 2015, 09:11
layout.addWidget(txt); //<<<< remove '&'

NoRefer
12th March 2015, 11:07
layout.addWidget(txt); //<<<< remove '&'

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 11014 :)
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:

#ifndef STRUCTS_H
#define STRUCTS_H



class structs
{
public:
structs();
void setRun(int i);
int getRun();


private:
int isRun = 0;
};

#endif // STRUCTS_H
structs.cpp

#include "structs.h"

/*struct::structs()
{
}*/

void structs::setRun(int i)
{
isRun = i;
}

int structs::getRun()
{
return isRun;
}


I want to use them here:

berechnung.cpp (math)
if (structs::getRun() == 1) {
if (i == 7){
if (hypo >= 0){
if (richtung > 22.5 && richtung < 67.5 && _person->positionX < _person->vec1){
_person->positionX = _person->positionX + 1;
qDebug() << "I think I have just done a step to the RIGHT->!!!";
}
else if (richtung > 22.5 && richtung < 67.5 && _person->positionX > _person->vec1){
_person->positionX = _person->positionX - 1;
qDebug() << "I think I have just done a step to the <-LEFT!!!";
}
else if (richtung > 0 && richtung < 22.5 && _person->positionY > _person->vec2){
_person->positionX = _person->positionY - 1;
qDebug() << "I think I have just done a step to the TOP!!!";
}
else if (richtung > 0 && richtung < 22.5 && _person->positionY < _person->vec2){
_person->positionX = _person->positionY + 1;
qDebug() << "I think I have just done a step to the BOTTOM!!!";
}

}
richtung = 0;

}
structs::setRun(0);
}
and here:

=display.cpp
void display::on_run_clicked()
{
structs::setRun(1);
qDebug() << "Congrats, you managed to click the Bottom: " << structs::getRun();
}


Any idea how to make this work?

anda_skoa
12th March 2015, 11:35
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,
_