Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SFML 2.1 + 5.0.2] sfml canvas update doesn't work in MainWindow's Qframe  (Read 863 times)

0 Members and 1 Guest are viewing this topic.

Shaoboy

  • Newbie
  • *
  • Posts: 1
    • View Profile
Hi devs!

First of all i'd like to pin down that i'm a newbie both in Qt and SFML but somehow i've managed to set them properly and starting a small project for a university class.
There are two possibilities, first when i want to display and update something in a newly created Qframe object. In this case everything works fine both drawing and updates.

But in the second case when this Qframe object belongs to the MainWindow then update doesn't work. To tell the truth it has no affect on the project it's just a flaw, one main window is nicer than two seperated at least in my opinion

So i hope you can give some advice:

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QCloseEvent>
#include <QFrame>
#include "mycanvas.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->textEdit->setText("Logging...");
    this->setWindowTitle("Simulator v0.8");

    socket = new QUdpSocket(this);
    socket->bind(QHostAddress("192.168.1.100"), 8081);
    connect(socket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));


// 1st case, both updating and drawing work perfectly but i have two seperate window

    /*QFrame* MainFrame = new QFrame;
    MainFrame->setWindowTitle("Simulator");
    MainFrame->resize(1024, 500);
    MainFrame->show();
    MyCanvas* SFMLView=new MyCanvas(MainFrame,QPoint(60,20), QSize(400,400));
    MyCanvas* SFMLView2=new MyCanvas(MainFrame,QPoint(564,20), QSize(400,400));
    SFMLView->show();
    SFMLView2->show();
    SFMLView->line.setRotation(-190);
    SFMLView->OnUpdate();
    SFMLView->sf::RenderWindow::display();
    SFMLView->show();*/



// 2nd case, update doesn't work in this one
    MyCanvas* SFMLView=new MyCanvas(ui->frame0,QPoint(60,20), QSize(400,400));
    MyCanvas* SFMLView2=new MyCanvas(ui->frame0,QPoint(564,20), QSize(400,400));
    SFMLView->show();
    SFMLView2->show();
    SFMLView->line.setRotation(-190);
    SFMLView->line3.setSize(sf::Vector2f(25, 5));
    SFMLView->OnUpdate();
    SFMLView->sf::RenderWindow::display();
    SFMLView->show();
    SFMLView2->show();

}

MainWindow::~MainWindow()
{
    delete ui;
    socket->close();
}


 

QFSMLCanvas.cpp
#include "QSFMLCanvas.h"
#ifdef Q_WS_X11
#include <Qt/qx11info_x11.h>
#include <X11/Xlib.h>
#endif
#include <iostream>
QSFMLCanvas::QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime) : QWidget(Parent),
myInitialized (false)
{
// Setup some states to allow direct rendering into the widget
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
// Set strong focus to enable keyboard events to be received
setFocusPolicy(Qt::StrongFocus);
// Setup the widget geometry
move(Position);
resize(Size);
// Setup the timer
myTimer.setInterval(FrameTime);
}
QSFMLCanvas::~QSFMLCanvas() {}
void QSFMLCanvas::showEvent(QShowEvent*)
{
if (!myInitialized)
{
#ifdef Q_WS_X11
    XFlush(QX11Info::display());
#endif
// Create the SFML window with the widget handle
    sf::RenderWindow::create(this->winId());
// Let the derived class do its specific stuff
    OnInit();
// Setup the timer to trigger a refresh at specified framerate
    /*connect(&myTimer, SIGNAL(timeout()), this, SLOT(repaint()));
    myTimer.start();*/

    myInitialized = true;
}
}
QPaintEngine* QSFMLCanvas::paintEngine() const
{
return 0;
}
void QSFMLCanvas::paintEvent(QPaintEvent*)
{
// Let the derived class do its specific stuff
OnUpdate();
// Display on screen
sf::RenderWindow::display();
}
void QSFMLCanvas::OnInit() {}
void QSFMLCanvas::OnUpdate() {}
 

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QUdpSocket>
#include <QDebug>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
   
public:
    explicit MainWindow(QWidget *parent = 0);
    QByteArray buffer;
    ~MainWindow();
    void SendData();

public slots:
    void processPendingDatagrams();

private slots:

    void on_close_clicked();

    void on_clear_clicked();

    void on_send_clicked();

private:
    Ui::MainWindow *ui;
    QUdpSocket *socket;
    void closeEvent (QCloseEvent *event);

};

#endif // MAINWINDOW_H
 

QFSMLCanvas.h
#ifndef QSFMLCANVAS_H
#define QSFMLCANVAS_H
#include <SFML/Graphics.hpp>
#include <QWidget>
#include <QTimer>
class QSFMLCanvas : public QWidget, public sf::RenderWindow
{
//Q_OBJECT
public:
explicit QSFMLCanvas(QWidget *parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime = 0);
virtual void showEvent(QShowEvent*);
virtual QPaintEngine* paintEngine() const;
virtual void paintEvent(QPaintEvent*);
virtual ~QSFMLCanvas();
virtual void OnInit();
virtual void OnUpdate();
private:
QTimer myTimer;
bool myInitialized;
};
#endif // QSMLCANVAS_H
 

I hope i was understandable and you can help me. Thank you!