Qt reimplement function - c++

I need to subclass qt class and reimplement the virtual function.
Say that i have the QLCDNumber class and I want to reimplement the function that set the position of the number when resize the LCD number screen, how to achieve that?
I read that by inhert a class from QLCDNumber and reimplement the function,
but where to get that function code so that I can edit what needed in that function? I read the documentation but it explains the function uses and not show its code. Example image:
I mentioned the QLCDNumber class as example, I need to know the prosses of reimplemnt a virtual Qt class function.

For this particular problem you can use this type of approach where you can create an object which inherits QWidget and implement the paintEvent() of the class like this:
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
protected:
void paintEvent(QPaintEvent *pe);
signals:
public slots:
};
and in the paintEvent()
void MyWidget::paintEvent(QPaintEvent *pe)
{
QPainter *painter = new QPainter(this);
painter->begin(this);
// draw what you want here using painter
// and consider your different layouts
painter->end();
}

Related

Qt update an image in the UI through callback

I have a function streamCamera(*callback) where the callback is void callback(Image) which can be used to preview the stream in any UI framework we want.
I was able to stream with this function in a opencv imshow window
void callback(image)
{
cv::imshow("image", image);
waitKey(1);
}
while the imshow is a static function and was able to stream.
Is there anything similar i can do with Qt?
What I tried,
I have a class MainWindow which inherits QMainWindow, inside the class i have attached a QGraphicsPixmapItem pixImage into the ui with ui->graphicsView->scene()->addItem(&pixImage);
since the pixImage is not accessible to the callback function, cause the Mainwindow object is not global. I was not able to set image in the pixmap;
So i ended up doing a hack, just created a global pointer a QGraphicsPixmapItem *pixImagePtr and attached the pixImage before starting the streamCamera(*callback) and inside the callback i use the pixImagePtr to set the image GUI, and it worked.
But this hack seems like not the right way to do, so it would be helpful if some qt experts shed some light on it.
Sounds like something that should be done with signals.
Let's say you have your MainWindow and Worker class.
Worker wants to show some image inside the MainWindow, you can do it like this.
class MainWindow : public QMainWindow {
Q_OBJECT
// Other members
public slots:
void showImage(const QPixmap &pixmap) {
pixImage.setPixmap(pixmap);
}
};
class Worker : public QObject {
Q_OBJECT
public:
Worker(MainWindow* parent) : QObject{parent} {
connect(this, &Worker::pixmapChanged,
parent, &MainWindow::showImage);
}
signals:
void pixmapChanged(QPixmap pixmap);
}
From now inside Worker you should be able to use pixmapChanged signal to change MainWindow image like so.
const QPixmap pixmap = getNewPixmap();
emit pixmapChanged(pixmap);

Resize QMainwindow to fit Qdockwidget size

I have a QMainWindow and a QDockWidget nested inside this.
I show some graphs, so the QDockWidget expands but the QMainWindow keeps it's initial size so i have to resize it using my mouse.
So, how can i make a QMainWindow resize to QDockWidget size every time?
It was easy at the end.
I take the Qsize of my QDockWidgets and i resize my QMainWIndow to this.
For example i have 2 QDockWidget side by side so what i do is
QSize siz = Dock->size();
QSize siz2 = Dock2->size();
resize(siz.width()+siz2.width(),siz.height);
You might want to rewrite the resizeEvent function of the QDockWidget widget. For that you need to subclass QDockWidget.
class MYDockwidget : public QDockWidget
{
Q_OBJECT
public:
MYDockwidget(QWidget *parent = 0):
QDockWidget(parent)
{}
protected:
void resizeEvent(QResizeEvent *event)
{
QDockWidget::resizeEvent(event);
// Calulate Main window size here.
// the main window is accesible
// through the parent property.
}
};
This approach works, but binds the QDockWidget's resizeEvent to the QMainWindow. The proper solution is to emit a signal when the size of the QDockWidget change.
For that you will need to define a custom signal and of course you want that signal with information about the event in question, hence our signal will be emited with a QSize argument.
class MYDockwidget : public QDockWidget
{
Q_OBJECT
public:
MYDockwidget(QWidget *parent = 0):
QDockWidget(parent)
{}
signals:
void sizeChanged(QSize);
protected:
void resizeEvent(QResizeEvent *event)
{
QDockWidget::resizeEvent(event);
emit sizeChanged(event->size());
}
};
After that you can write code like:
// Inside your main window.
public slots:
void on_dock_size_changed(QSize)
MYDockwidget *dock = new MYDockwidget(this);
connect(dock, SIGNAL(sizeChanged(QSize)), this, SLOT(on_dock_size_changed(QSize)));
void on_dock_size_changed(QSize size)
{
// resize your main window here.
}
Disadvantage:
You will need to set the QDockWidget's properties by hand (programmatically) unless you manage your self to insert your custom widget as a QTDesigner plugin.

Qt: Creating a QDoubleSlider

I want to connect a QDoubleSpinBox with a QSlider like this:
QObject::connect(ui->myDoubleSpinBox, SIGNAL(valueChanged(double)),
ui->mySlider, SLOT(setValue(double)));
QObject::connect(ui->mySlider, SIGNAL(valueChanged(double)),
ui->myDoubleSpinBox, SLOT(setValue(double)));
This won't work, for a QSlider only handles int values. So I think i need to add a custom slot to QSlider.
I thought about creating a new class derived from QSlider and then implementing the slot, like this:
QDoubleSlider.hpp
#ifndef QDOUBLESLIDER_H
#define QDOUBLESLIDER_H
#include <QSlider>
class QDoubleSlider : public QSlider
{
Q_OBJECT
public:
explicit QDoubleSlider(QObject *parent = 0);
signals:
public slots:
void setValue(double givenValue);
};
#endif // QDOUBLESLIDER_H
QDoubleSlider.cpp
#include "qdoubleslider.h"
QDoubleSlider::QDoubleSlider(QObject *parent) :
QSlider(parent)
{
}
void QDoubleSlider::setValue(double givenValue)
{
// code
}
Now I have two problems:
The compiler complains about an invalid conversion from QObject* to QWidget* in the constructor.
I don't know how setValue works and thus I don't even know how to implement that slot.
Any ideas?
parent needs to be a QWidget*, just as the error states
Slots work like regular member functions. You should probably store an exact double as a member and set the underlying slider to the appropriate integer equivalent. Remember to only send valueChanged signals if the value really changes.
Furthermore you should probably inherit from QWidget instead and have a QSlider as a child, as you do not want users of your class to change the integer range of your internal slider.
just simply replace your QObject to QWidget, because QSlider constructor is
QSlider ( QWidget * parent = 0 )
you'd better have a new slot named setDoubleValue(double givenValue) and connect to this slot. in this slot, it is simple. like
void QDoubleSlider::setDoubleValue(double givenValue)
{
setValue(static_cast<int>(givenValue));
}

Create a custom slot in C++, Qt5

in python we write custom slots quite easily by passing in the function to be called when a signal is generated.
While in C++ connect function requires us to pass the address of the slot function or so i figured. How do i do that. I tried using this but did'nt work.
Python code::
class imviu(QtGui.QWidget):
def __init__(self):
super(imvui,self).__init__()
self.btn=QtGui.QPushButton('Browse')
btn.clicked.connect(self.openimg)
def openimg(self):
#do something
C++ code::
class imviu: public QWidget
{
public:
imviu(QWidget *parent=0);
QPushButton *btn=new QPushButton("Browse");
void openimg(void);
};
imviu::imviu(QWidget *parent)
:QWidget(parent)
{
connect(btn, SIGNAL(clicked()),this,SLOT(openimg()));//this does'nt work:QObject::connect: No such slot QWidget::openimg()
}
void imviu::openimg()
{
//do something
}
In order to use signals and slots, you need to have the Q_OBJECT macro in your class as well as identifying which functions should be the signals and the slots. Have a look at the documentation for a more in-depth explanation.
After this, you need to set up the project file so that MOC can generate the necessary code.
Your class definition should look like this:
class imviu: public QWidget
{
Q_OBJECT
public:
imviu(QWidget *parent=0);
public slots:
void openimg();
private:
QPushButton *btn;
};

Accessing a widget from outside of its class?

In Qt Creator, I have a couple of widgets declared like so:
Header File:
class MapViewer : public QGraphicsView
{
Q_OBJECT
public:
explicit MapViewer(QGraphicsScene *scene, QWidget *parent = 0);
~MapViewer();
public slots:
void mousePressEvent(QMouseEvent *event);
};
// Declaration for the map editor window.
class MapEditor : public QMainWindow
{
Q_OBJECT
public:
explicit MapEditor(QWidget *parent = 0);
~MapEditor();
public:
QLayout *editorLayout;
QPushButton *btn;
QGraphicsScene *mapScene;
MapViewer *mapView;
private:
Ui::MapEditor *ui;
};
CPP File:
MapEditor::MapEditor(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MapEditor)
{
ui->setupUi(this);
this->setWindowTitle("2DXY :: Map Editor");
this->setGeometry(10,10,1170,750);
editorLayout = new QVBoxLayout; // Create a new layout
this->setLayout(editorLayout); // Set the widget's layout to our newly created layout.
mapScene = new QGraphicsScene(); // Create a new graphics scene to draw upon.
mapView = new MapViewer(mapScene,this); // Create a new graphics view to display our scene - set its parent to 'this' so that it doesn't open in a new window.
mapView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mapView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mapView->setGeometry(20,20,1178,546); // Width first, then height.
AND:
void MapViewer::mousePressEvent(QMouseEvent *event)
{
// Show an empty message box, just to check that the event handler works!
QMessageBox *notification = new QMessageBox();
notification->show();
notification->exec();
// Some how access the same QGraphicsScene and View (mapScene, mapView) as above, so
// I can update their contents on the open form / window.
}
And as you can see, I wish to access the Graphics Scene again to update it, then redraw it (or whatever). But I'm not able to access the graphics scene at all, despite a few hours of trial and error with declaring widgets in different ways.
I know that the listener itself works, because if it's set to open a new message box, or output to the debug window, then it works, it's just that I can't access the widgets I've already defined.
I feel that there is a (relatively) easy solution to this problem, and that I'm just missing something obvious.
You passed the QGraphicsScene to your MapRender object's constructor. What do you do with the scene within its constructor? Ideally, you should be storing it as a data member of MapRender. For example:
class MapRender {
public:
MapRender(QGraphicsScene* scene)
: scene_(scene)
{
}
public slots:
void mousePressEvent(QMouseEvent *event);
private:
QGraphicsScene* scene_;
}
Now in your implementation of mousePressEvent, you can access to the scene member:
void MapRender::mousePressEvent(QMouseEvent *event) {
int CursorX = event->globalX();
int CursorY = event->globalY();
QGraphicsRectItem *clickedBox = new QGraphicsRectItem(40,40,32,32);
clickedBox->setBrush(QBrush(Qt::blue));
scene_->addItem(clickedBox);
}
Keep in mind you should ideally be putting the implementation of the constructor in your cpp file, but my example does it in the declaration for brevity.
void MapViewer::mousePressEvent(QMouseEvent *event)
{
// Show an empty message box, just to check that the event handler works!
QMessageBox *notification = new QMessageBox();
notification->show();
notification->exec();
// To add something whenever the user clicks, you don't need the view,
// just the scene.
scene()->addItem( new MyItem() );
}
Remember MapViewer derives from QGraphicsView and the view must know about the scene it belongs to - so it has a scene() method to return it, which you inherited.