How Use QTimer Command - loops

I have a problem in using QTimer command.
I dont have any syntax error, but i have 2 error in qglobal.h and qobjectdefs_impl.h and i dont understand them.
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
void MainWindow::updatewindow()
{
Mat frame;
capture >> frame;
cvtColor(frame, frame, cv::COLOR_BGR2RGB);
QImage image((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
QPixmap temp_img = QPixmap::fromImage(image);
ui->label2->setPixmap(temp_img);
}
void MainWindow::on_pushload_clicked()
{
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, SLOT(updatewindow()));
timer->start(20);
}
I have a problem in using QTimer command.
I dont have any syntax error, but i have 2 error in qglobal.h and qobjectdefs_impl.h and i dont understand them.
and mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPixmap>
#include <QTimer>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QPixmap Iblackwhite,IMG_Color{};
QImage image {};
cv::VideoCapture capture{};
private slots:
void updatewindow();
void on_pushload_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
please help me to solve my problem.

Use &MainWindow::updateWindow instead of SLOT(updatewindow()).

Related

Qt app (with while loop) still running after closing MainWindow

I have this sample code that reproduces the problem I am dealing with.
//main.cpp
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
widget w;
w.show();
return a.exec();
}
--
//widget.h
#pragma once
#include <QMainWindow>
#include <QPushButton>
#include <QApplication>
#include <QLayout>
class widget : public QMainWindow
{
Q_OBJECT
public:
QApplication* application;
widget(QWidget *parent = nullptr);
QVBoxLayout *mainLayout;
QPushButton *startButton;
QWidget *centralWidget;
~widget();
public slots:
void onStartButtonPressed();
private:
void start();
bool loop;
};
--
//widget.cpp
#include "widget.h"
widget::widget(QWidget *parent)
: QMainWindow(parent)
{
application = static_cast<QApplication*>(QApplication::instance());
mainLayout = new QVBoxLayout();
loop = false;
startButton = new QPushButton("START");
mainLayout->addWidget(startButton);
connect(startButton, SIGNAL(clicked()),this,SLOT(onStartButtonPressed()));
centralWidget = new QWidget();
centralWidget->setLayout(mainLayout);
this->setCentralWidget(centralWidget);
this->showMaximized();
this->setWindowTitle("Widget");
}
void widget::onStartButtonPressed()
{
loop = !loop;
startButton->setText(loop? "STOP" : "START");
start();
}
void widget::start()
{
while(loop)
{
application->processEvents();
}
}
widget::~widget()
{
loop = false;
}
This is a simple Qt application with a QPushButton that when pressed set a boolean flag to true and starts a loop. When the main widget is closed the destructor sets boolean flag to false and the loop should break.
However when loop is running the destructor seems not been called, while when loop is not running it is called normally and the app quit accordingly.
Of course I call application->processEvents() to prevent freezing, but I don't know why the destructor is not called at loop running.

How to call a native C function from Flutter?

I want to call 'hello_world' function from Flutter.
#include <stdio.h>
#include "hello.h"
__attribute__((visibility("default"))) __attribute__((used))
int main()
{
hello_world();
return 0;
}
void hello_world()
{
printf("Hello World\n");
}
I have tried this way in my plugin.dart:
final DynamicLibrary helloLib = Platform.isAndroid
? DynamicLibrary.open("libhello.so")
: DynamicLibrary.process();
final void Function() hello =
helloLib
.lookup<NativeFunction<Void Function()>>("hello_world")
.asFunction();
In my main.dart file I tried this way:
body: Center(
child: Text('Running on: ${hello}'),
But it shows void instead of showing the result. How to solve this problem?
Thanks in advance.

object hierarchy functions

My task would be to make this object hierarchy and have a sum of all file sizes in the file system.
If I understand correctly this would be a multi level inheritance, but I'm not sure how to write a function what can run through every lower-level object and sum them up.
#include <iostream>
using namespace std;
class File{
public:
//File(int x){filesize=x;}
void setSize(int x){filesize=x;}
void showsize(){cout<<filesize<<endl;}
int getsize(){return filesize;}
private:
int filesize;
};
class Directory : public File {
public:
void SumDir(){
}
private:
int sumofdir=0;
};
int main()
{
File b;
b.setSize(416);
b.showsize();
return 0;
}
You could have the Directory class have a std::vector of File*. Then it would be just a simple walkthrough of each of these directory entries and in turn, invoke the getSize() on them. getSize() could be a polymorphic function that just either gets the size of the File if it's a regular file or the size of the entire directory if it's an instance of a Directory. Something on these lines:
#include <iostream>
#include <vector>
class File{
public:
explicit File(int x): fileSize(x){}
void setSize(int x){
fileSize=x;
}
void showSize() const{
std::cout<<fileSize<<'\n';
}
virtual size_t getSize() const{
return fileSize;
}
virtual ~File() = default;
private:
int fileSize;
};
class Directory : public File {
public:
Directory() : File(0){} //Directory is just a File of 0 size
size_t getSize() const override{
size_t size = 0;
for(auto const& file: dirEntries) {
size += file->getSize();
}
return size;
}
void addFile(File* file) {
if(file != nullptr) {
dirEntries.push_back(file);
}
}
private:
std::vector<File*> dirEntries;
};
int main() {
Directory child;
File f1{10}, f2{20};
child.addFile(&f1);
child.addFile(&f2);
Directory parent;
File f3{30};
parent.addFile(&f3);
parent.addFile(&child);
std::cout<<parent.getSize(); //60
}

Simple video player in QT playing audio only

I written a program that is supposed to show video as well as audio using QT. However I cannot work out why the video is not playing, while the audio works fine.
vidpb.pro
QT += core gui multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = vidpb
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
CONFIG += mobility
MOBILITY = multimedia
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class QMediaPlayer;
class QVideoWidget;
class MainWindow : public QMainWindow
{
Q_OBJECT
QMediaPlayer *player;
QVideoWidget *widget;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QtMultimedia>
#include <qvideowidget.h>
#include "qmediaplayer.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
player->setMedia(QUrl::fromLocalFile("/sdcard/Movies/video1.mp4"));
widget = new QVideoWidget(this);
player->setVideoOutput(widget);
widget->show();
setCentralWidget(widget);
this->show();
player->play();
}
MainWindow::~MainWindow()
{
delete ui;
}

shared library how to link to a symbol?

I have:
car.cc
#include "car.h"
#include <iostream>
using namespace std;
extern "C" Car* create_object()
{
return new Car;
}
Car::Car() {
this->maxGear = 2;
this->currentGear = 1;
this->speed = 0;
}
void Car::shift(int gear) {
if (gear < 1 || gear > maxGear) {
return;
}
currentGear = gear;
}
void Car::brake() {
speed -= (5 * this->getCurrentGear());
std::cout<<"THE SPEED IS:" <<speed<<std::endl;
}
extern "C" void destroy_object( Car* object )
{
delete object;
}
car.h
#ifndef VEHICLES_CAR_H
#define VEHICLES_CAR_H
// A very simple car class
class Car {
public:
Car();
void shift(int gear);
void accelerate();
void brake();
private:
int maxGear;
int currentGear;
int speed;
};
#endif /* VEHICLES_CAR_H */
test.cc
#include "/home/car.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
/* on Linux, use "./myclass.so" */
void* handle = dlopen("/usr/lib/libCarTest.so", RTLD_LAZY);
int (*result)(int);
if (!handle)
{
}
/*dlsym(handle,"accelerate");
cout<<"IN HERE: "<<endl;
dlsym(handle,"brake");
dlclose(handle);*/
Car* (*create)();
void (*destroy)(Car*);
dlerror();
create = (Car* (*)())dlsym(handle, "create_object");
destroy = (void (*)(Car*))dlsym(handle, "destroy_object");
Car* carr = (Car*)create();
carr->brake();
destroy( carr );
dlclose(handle);
/*
Car carr;
carr.brake();
* compilation g++ test.cpp -o tst /path/libcar.so
*/
return 0;
}
After creating libMyLib.so and install it in /usr/lib i've tried to compile test.cc using: g++ test.cc -o tst -ldl. WHY do i need to include -lMyLib? is there a way to compile the code without libMyLib.so? Secondly why dlsym(handle,"brake") is not working? If i change dlsym (Car* (*).... with dlsym(handle,"brake") i get nothing. why?
Appreciate
WHY do i need to include -lMyLib?
Because you need to link to the Car::brake method.
Secondly why dlsym(handle,"brake") is not working?
Because there is no brake symbol. The method Car::brake has a complicated mangled (implementation-defined) name. You can see this in the output of nm -D.
AFAIK, you can solve it by
making all the methods of Car virtual (they will be called through a pointer, so no linking will be needed)
doing it the old C way, ie. export a free function brake() that would call the Car::brake method from the .so
making all the public methods of Car inline and defining them in the header.
emulating the virtual table approach (as we do it in C)
Combining the last two approaches:
class Car {
public:
void brake() { brake_impl(this); }
private:
void (*brake_impl)(Car*);
void do_brake(); // this would be the actual implementation
Car() : brake_impl([] (Car* c){ c->do_brake(); }) { ... }
};
Of course you could split the implementation and the interface so it's not such a mess.

Resources