PDA

View Full Version : call drawing function out of paintGL



CyrilQt
13th June 2012, 15:58
Hi all,
Here, I joined a QGLWidget class QGLWidget in a main window. The glwidget displays well what is contained in the method paintGL ().
But I want to ensure that these methods, those contained in paintGL, are called from the mainwindows by the user according to his need to display.
Here are bits of code most relevant to get an idea of the situation:

mainwindow.cpp:


mainwindow::mainwindow()
{
m_Central= new QWidget;
m_layout = new QGridLayout;
m_Central->setLayout(m_layout);

m_carto = new GLWidget
m_carto->displayRedTriangle();
m_layout->addwidget(carto);

setCentralWidget(m_Central);
}


glwidget.cpp:


void GLWidget::paintGL()
{
// some initializing boring parameters
// Setting displayRedTriangle() here will show the red triangle in the user interface.
}

void GLWidget::displayRedTriangle()
{
glBegin(GL_TRIANGLES)
glColor3f(1.0f,0.0f,0.0f)
glVertex3i(-1,0,2);
glVertex3i(1,0,2);
glVertex3i(0,0,0);
glEnd();
}


So the question is, how to use and make work the displayRedTriangle() from any place that is not directly paintGL?
Thanks for your help,
Best regards.

Cyril

wysota
13th June 2012, 16:37
Why do you want to do that?

CyrilQt
13th June 2012, 18:27
good evening,

This triangle is a kind of cursor to set on a map at a perticular position.
I have the map ( i used a .mnt and a .tga pictures for the texture) and til this point everything is fine.
My user interface built, in the mainwindows ask users to enter new datas linked with a postgresql database. One of the table contains the coordinates of other items like adresses etc...
I would like to call them (the cursors through a function for each display preferences of the users) this cursor should be called by a user, and displayed at the correct position. I would like to be able to add points from the UI i built and see them on the map at the same time...
I thanks you for your attention, and hope to find a solution.
Best regards.

Cyril

wysota
13th June 2012, 18:55
So just call update() and draw that cursor as part of paintGL().

CyrilQt
13th June 2012, 19:03
Ho yes, yes, I thought of it at the first time. I set it like this:


mainwindow::mainwindow()
{
m_Central= new QWidget;
m_layout = new QGridLayout;
m_Central->setLayout(m_layout);

m_carto = new GLWidget
m_carto->displayRedTriangle();
m_carto->updateGL();
m_layout->addwidget(carto);

setCentralWidget(m_Central);
}


but it didn't work...or it works, but I can see nothing, no changes.

I thank you for your reply and proposal, I'm really short of idea though i guess it's not a so difficult problem. Maybe should i have post it in the newbies forum?

Cyril

wysota
13th June 2012, 19:24
No, that's wrong. You should store the coordinates for the triangle, then call update() and then in paintGL() use those stored coordinates to draw that triangle.

CyrilQt
13th June 2012, 19:36
No, I'm really sorry but I do not understand.
I may have wanted to be too concise, or I have not explained my problem clearly.
I want to display as much cursor that the user wishes, as it can have the choice to withdraw according to the elements he wants to view.
To this, I thought to set the function into a slot displayRedtriangle issued by a signal such as those emitted by QCheckBox.
For this I thought I would call the function as Defois triangle as necessary, and display all those that the user wishes.
I should have the wrong path :'(

wysota
13th June 2012, 19:50
I want to display as much cursor that the user wishes
So store a list of coordinates.

CyrilQt
13th June 2012, 20:10
Ok, i will try something like that :)
but i'm not sure how to proceed, we will see
thanks!!

CyrilQt
14th June 2012, 20:44
I feel very sorry, but I didn't find what you try to explain me.
I stored all my points in a table, and i'm able to display all of them setting the relevant function showMyTriangles ( 3dPoints*tab) in PaintGL().
All points are loaded and display.
But i cannot have any interaction with it from the mainwindow interface. For instance, i would like to display my house and my school, and have only two triangles. As well I would like to make them disapear with the appropriate Qcheckbox.
I'm really in need, and i really tried my best with your indications.
Best regards.

Cyril

wysota
14th June 2012, 21:24
Here is a simple example, adapt it to your needs. I'm not commenting it on purpose so that you have to put some effort in understanding it.


#include <QtGui>

class Widget : public QWidget {
Q_OBJECT
public:
Widget(QWidget *parent = 0) : QWidget(parent) {}
void addEntry(QString label, QPoint pos) {
Entry e;
e.label = label;
e.pos = pos;
QFontMetrics fm = fontMetrics();
QRect r = fm.boundingRect(label);
r.translate(e.pos);
e.boundingBox = r;
e.selected = false;
m_entries << e;
update();
}

void removeEntry(int which) {
m_entries.removeAt(which);
update();
}

int indexAt(QPoint pos) const {
for(int i=0; i< m_entries.count(); ++i) {
const Entry &e = m_entries.at(i);
if(e.boundingBox.contains(pos))
return i;
}
return -1;
}
protected:
void paintEvent(QPaintEvent *pe) {
QPainter p(this);
foreach(Entry e, m_entries) {
p.save();
if(e.selected) {
p.setPen(Qt::red);
}
p.drawText(e.pos, e.label);
p.restore();
// p.save();
// p.setPen(Qt::red);
// p.drawRect(e.boundingBox);
// p.restore();
}
}
void mouseReleaseEvent(QMouseEvent *me) {
int idx = indexAt(me->pos());
if(idx < 0) return;
m_entries[idx].selected = !m_entries[idx].selected;
update();
}

private:
struct Entry {
QString label;
QPoint pos;
QRect boundingBox;
bool selected;
};

QList<Entry> m_entries;
};


#include "main.moc"

int main(int argc, char **argv) {
QApplication app(argc, argv);
Widget w;
w.show();
w.addEntry("Home", QPoint(100, 100));
w.addEntry("School", QPoint(300, 200));
w.addEntry("Work", QPoint(500, 20));
return app.exec();
}

CyrilQt
14th June 2012, 23:11
Thanks!!
I will try this tomorrow,
I understood part of the code and see how it may work *hope*
:)

CyrilQt
16th June 2012, 10:17
Hi,
I'm getting some good results due to your advice. I will write it back soon, it may help someone later.