Skip to content

Commit

Permalink
oops, GUI elements may only be updated from main thread
Browse files Browse the repository at this point in the history
otherwise may lead to memory corruption; hopefully this also fixes a random segfault when calling QLabel::setText() in MainWindow::OnSeek()
  • Loading branch information
derselbst committed Jun 13, 2016
1 parent b509139 commit 2dcccaa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
39 changes: 24 additions & 15 deletions src/GUI/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@
#include <utility> // std::pair
#include <cmath>

void MainWindow::onSeek(void* ctx, frame_t pos)
void MainWindow::callbackSeek(void* context, frame_t pos)
{
MainWindow* context = static_cast<MainWindow*>(ctx);
MainWindow* ctx = static_cast<MainWindow*>(context);
QMetaObject::invokeMethod( ctx, "slotSeek", Qt::QueuedConnection, Q_ARG(long long, pos ) );
}

void MainWindow::callbackCurrentSongChanged(void * context)
{
MainWindow* ctx = static_cast<MainWindow*>(context);
QMetaObject::invokeMethod( ctx, "slotCurrentSongChanged", Qt::QueuedConnection);
}

QSlider* playheadSlider = context->ui->seekBar;
void MainWindow::slotSeek(long long pos)
{
QSlider* playheadSlider = this->ui->seekBar;
bool oldState = playheadSlider->blockSignals(true);
playheadSlider->setSliderPosition(pos);
playheadSlider->blockSignals(oldState);

const Song* s = context->player->getCurrentSong();
const Song* s = this->player->getCurrentSong();
if(s==nullptr)
{
return;
Expand All @@ -40,25 +50,24 @@ void MainWindow::onSeek(void* ctx, frame_t pos)
if((void*)temp.c_str() == (void*)strTimePast.constData())
puts("asdf");

context->ui->labelTimePast->setText(strTimePast);
this->ui->labelTimePast->setText(strTimePast);
}

{
temp = framesToTimeStr(s->getFrames()-pos, s->Format.SampleRate);
QString strTimeLeft = QString("-") + QString::fromStdString(temp);
context->ui->labelTimeLeft->setText(strTimeLeft);
this->ui->labelTimeLeft->setText(strTimeLeft);
}
}

void MainWindow::onCurrentSongChanged(void* context)
void MainWindow::slotCurrentSongChanged()
{
MainWindow* ctx = static_cast<MainWindow*>(context);
QSlider* playheadSlider = ctx->ui->seekBar;
QSlider* playheadSlider = this->ui->seekBar;

const Song* s = ctx->player->getCurrentSong();
const Song* s = this->player->getCurrentSong();
if(s==nullptr)
{
ctx->setWindowTitle("ANMP");
this->setWindowTitle("ANMP");

bool oldState = playheadSlider->blockSignals(true);
playheadSlider->setSliderPosition(0);
Expand All @@ -71,11 +80,11 @@ void MainWindow::onCurrentSongChanged(void* context)
QString interpret = QString::fromStdString(s->Metadata.Artist);
if(title == "" || interpret == "")
{
ctx->setWindowTitle(QString::fromStdString(s->Filename) + " :: ANMP");
this->setWindowTitle(QString::fromStdString(s->Filename) + " :: ANMP");
}
else
{
ctx->setWindowTitle(interpret + " - " + title + " :: ANMP");
this->setWindowTitle(interpret + " - " + title + " :: ANMP");
}
playheadSlider->setMaximum(s->getFrames());
}
Expand All @@ -91,8 +100,8 @@ MainWindow::MainWindow(QWidget *parent) :
this->setWindowState(Qt::WindowMaximized);

// set callbacks
this->player->onPlayheadChanged += make_pair(this, &MainWindow::onSeek);
this->player->onCurrentSongChanged += make_pair(this, &MainWindow::onCurrentSongChanged);
this->player->onPlayheadChanged += make_pair(this, &MainWindow::callbackSeek);
this->player->onCurrentSongChanged += make_pair(this, &MainWindow::callbackCurrentSongChanged);

this->buildFileBrowser();
this->buildPlaylistView();
Expand Down
13 changes: 7 additions & 6 deletions src/GUI/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class MainWindow : public QMainWindow
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

static void callbackSeek(void*, frame_t pos);
static void callbackCurrentSongChanged(void*);

protected:
void resizeEvent(QResizeEvent* event) override;
void closeEvent(QCloseEvent* e) override;
Expand All @@ -47,11 +50,6 @@ class MainWindow : public QMainWindow
QFileSystemModel *drivesModel = new QFileSystemModel(this);
QFileSystemModel *filesModel = new QFileSystemModel(this);


static void onSeek(void*, frame_t);
static void onCurrentSongChanged(void *);


void buildPlaylistView();
void buildFileBrowser();
void createShortcuts();
Expand All @@ -61,7 +59,10 @@ class MainWindow : public QMainWindow

private slots:
friend class PlaylistModel;


void slotSeek(long long);
void slotCurrentSongChanged();

void on_actionAdd_Songs_triggered();
void on_actionPlay_triggered();
void on_actionStop_triggered();
Expand Down

0 comments on commit 2dcccaa

Please sign in to comment.