Qt: Subclassed spinbox does not count - c++

I try to subclass a Qt spinbox so i can get the mouse event. The code compiles, the events are working, but the spinbox doesn't count up or down. I have set the min value to -9999 and max value to 9999, default value is 100. But if i click the up or down button, nothing happens.
NewSpinBox.h:
#ifndef NEWSPINBOX_H
#define NEWSPINBOX_H
#include <QObject>
#include <QWidget>
#include <QEvent>
#include <QMouseEvent>
#include <QSpinBox>
class NewSpinBox : public QSpinBox
{
Q_OBJECT
public:
NewSpinBox(QWidget *parent = 0);
~NewSpinBox();
public slots:
void mousePressEvent(QMouseEvent *mouseEvent);
void mouseReleaseEvent(QMouseEvent *mouseEvent);
};
#endif // NEWSPINBOX_H
NewSpinBox.cpp:
#include "newspinbox.h"
NewSpinBox::NewSpinBox(QWidget *parent) : QSpinBox(parent)
{
}
NewSpinBox::~NewSpinBox()
{
}
void NewSpinBox::mousePressEvent(QMouseEvent *mouseEvent)
{
if(mouseEvent->type() == QMouseEvent::MouseButtonRelease)
{
int i = 0;
i++;
}
}
void NewSpinBox::mouseReleaseEvent(QMouseEvent *mouseEvent)
{
if(mouseEvent->type() == QMouseEvent::MouseButtonRelease)
{
int i = 0;
i++;
}
}
Do I have to reimplement the count functions?

You must call the base class handler to get the default behavior:
void NewSpinBox::mousePressEvent(QMouseEvent *mouseEvent)
{
QSpinBox::mousePressEvent(mouseEvent);
// ... your code
}
void NewSpinBox::mouseReleaseEvent(QMouseEvent *mouseEvent)
{
QSpinBox::mouseReleaseEvent(mouseEvent);
// ... your code
}

Related

QGraphicsSceneHoverEvent not called while mouse is pressed

I have a scene that extends QGraphicsScene and has an event filter to handle all my scene logic and subsequent hover events etc happen correctly unless the mouse button is held down.
For example I have child elements in groups that have hoverEnterEvent and hoverLeaveEvent that get called correctly unless I'm holding the mouse. I've tried adjusting the code according to some answers and the documentation but I think I may be overlooking something really simple like a scene setting
The element header:
#ifndef IOELEMENT_H
#define IOELEMENT_H
#include <QGraphicsEllipseItem>
#include <QGraphicsItemGroup>
#include <QPen>
class IOElement : public QGraphicsEllipseItem {
public:
IOElement(QGraphicsItemGroup *parent=nullptr);
...
protected:
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
...
};
#endif // IOELEMENT_H
The element cpp:
#include "ioelement.h"
#include <QBrush>
#include <QDebug>
#include "../nodescene.h"
#include "../nodestyles.h"
IOElement::IOElement(QGraphicsItemGroup *parent) : QGraphicsEllipseItem(parent) {
...
setAcceptHoverEvents(true);
setFlags(QGraphicsItem::GraphicsItemFlag::ItemIsSelectable |
QGraphicsItem::GraphicsItemFlag::ItemSendsScenePositionChanges |
QGraphicsItem::ItemSendsGeometryChanges);
}
void IOElement::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
NodeScene *nodeScene = dynamic_cast<NodeScene *>(scene());
nodeScene->setChildHover(this);
QPen pen;
pen.setColor(NodeStyles::Color::Pen_Hover_Normie);
pen.setWidth(2);
setPen(pen);
update();
QGraphicsEllipseItem::hoverEnterEvent(event);
}
void IOElement::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
NodeScene *nodeScene = dynamic_cast<NodeScene *>(scene());
nodeScene->setChildHover(nullptr);
QPen pen;
pen.setColor(NodeStyles::Color::IO_Pen_Normie);
pen.setWidth(1);
setPen(pen);
update();
QGraphicsEllipseItem::hoverLeaveEvent(event);
}
These hover routines work as expected and I can handle the rest of the mouse logic in the scene with a filter like so
The scene header:
#ifndef NODESCENE_H
#define NODESCENE_H
#include <QGraphicsScene>
class NodeScene : public QGraphicsScene {
Q_OBJECT
public:
using QGraphicsScene::QGraphicsScene;
explicit NodeScene(QObject *parent = nullptr);
...
IOElement * getChildHover() const;
void setChildHover(IOElement * hover=nullptr);
protected:
bool eventFilter(QObject *watched, QEvent *event);
private:
...
IOElement *hoverIO;
...
};
#endif // NODESCENE_H
The scene cpp:
#include "nodescene.h"
...
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QDebug>
NodeScene::NodeScene(QObject *parent) : QGraphicsScene(parent) {
...
// Events
installEventFilter(this);
}
IOElement * NodeScene::getChildHover() const { return hoverIO; }
void NodeScene::setChildHover(IOElement * hover) {
hoverIO = hover;
}
bool NodeScene::eventFilter(QObject *watched, QEvent *event) {
if(watched == this) {
QGraphicsSceneMouseEvent *mouseSceneEvent;
if(event->type() == QEvent::GraphicsSceneMousePress) { // Mouse Down
if(hoverIO) {
if(hoverIO->IsOutput()) {
activeConnection = new Connection(this, hoverIO->scenePos().x()+6, hoverIO->scenePos().y()+6);
} else {
// Check if connected first
qDebug() << "Grabbing intput";
}
}
}
else if (event->type() == QEvent::GraphicsSceneMouseRelease) { // Mouse Up
delete activeConnection;
activeConnection = nullptr;
}
else if (event->type() == QEvent::GraphicsSceneMouseMove) { // Mouse Move
mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
int x = mouseSceneEvent->lastScenePos().rx();
int y = mouseSceneEvent->lastScenePos().ry();
if(activeConnection) {
activeConnection->DrawTo(x, y);
}
}
QKeyEvent *keyEvent;
if(event->type() == QEvent::KeyPress) { // Key Press
keyEvent = static_cast<QKeyEvent *>(event);
qDebug() << keyEvent->key();
}
}
return QGraphicsScene::eventFilter(watched, event);
}
When the mouse buttons are pressed nothing at all triggers in the hover events

Why can't I construct my subclass in QT C++? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 months ago.
I am working on a project that utilizes a whole suite of custom widget that my company uses.
I am trying to subclass the QLineEdit QWidget so that I can override the default arrow key behavior. It should just behave like a regular QLineEdit.
I then want to use this custom QLineEdit (that I named DataLineEdit), in place of the standard QLineEdits that I dynamically created in another Widget, DataWidget.
I have followed tutorial after tutorial on subclassing, but cannot avoid the error:
undefined reference to "DataLineEdit::DataLineEdit(QWidget*)
My code is below.
datalineedit.h
#ifndef DATALINEEDIT_H
#define DATALINEEDIT_H
#include <QLineEdit>
#include <QWidget>
class DataLineEdit: public QLineEdit
{
Q_OBJECT
public:
explicit DataLineEdit(QWidget *parent = 0);
~DataLineEdit();
protected:
void keyPressEvent(QKeyEvent* event);
signals:
void leftRightOverrideKeyPress(int state);
private:
};
#endif // CUSTOMLINEEDIT_H
datalineedit.cpp
#include "datalineedit.h"
#include<QKeyEvent>
DataLineEdit::DataLineEdit(QWidget*parent) : QLineEdit(parent)
{
}
DataLineEdit::~DataLineEdit()
{
}
void DataLineEdit::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Left && this->cursorPosition() == 0)
{
emit leftRightOverrideKeyPress(0);
}
else if(event->key() == Qt::Key_Right && this->cursorPosition() == 2)
{
emit leftRightOverrideKeyPress(1);
}
else
{
QLineEdit::keyPressEvent(event);
}
}
datawidget.h
#define DATAWIDGET_H
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include <QLineEdit>
#include <QObject>
#include <QtCore>
#include <QtGui>
#include <QKeyEvent>
#include <QMainWindow>
#include "datalineedit.h"
extern int focusRow;
extern int focusCol;
extern int numRows;
extern int numBottomRowCols;
extern QLineEdit *focusedLineEdit;
namespace Ui{
class DataWidget;
}
class QLineEdit;
class QLabel;
class DataWidget : public QWidget
{
Q_OBJECT
public:
explicit DataWidget(QWidget *parent = 0);
~DataWidget();
QString data;
protected:
bool eventFilter(QObject *object, QEvent *event);
public slots:
void focusChanged(QWidget *old, QWidget *now);
void arrowKeyNavigation(int state);
void on_applyChangePushButton_clicked();
void setData(QString text);
QString getData();
private slots:
void updateDataGridVisibility();
void on_payloadLineEdit_textChanged(const QString &arg1);
void on_savePushButton_clicked();
void on_openPushButton_clicked();
void on_presetComboBox_currentIndexChanged(int index);
private:
Ui::DataWidget *ui;
QString currentFile = "";
};
#endif // DATAWIDGET_H
datawidget.cpp
#include "datawidget.h"
#include "datawidget.h"
#include "datalineedit.h"
#include <QGridLayout>
#include <QLineEdit>
#include <QGroupBox>
#include <QKeyEvent>
#include <QIntValidator>
int focusRow;
int focusCol;
int numRows;
int numBottomRowCols;
QLineEdit *focusedLineEdit;
enum key_directions
{
arrowLeft = 0,
arrowRight = 1,
arrowUp = 2,
arrowDown = 3
};
enum presets
{
preset1 = 0,
preset2 = 1,
preset3 = 2,
preset4 = 3,
preset5 = 4,
clean = 5,
zeroes = 6
};
DataWidget::DataWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::DataWidget)
{
ui->setupUi(this);
this->installEventFilter(this);
connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(focusChanged(QWidget*,QWidget*)));
//payloadLineEdit
ui->payloadLineEdit->setValidator(new QIntValidator(0,64, this));
ui->payloadLineEdit->setFixedSize(20,20);
ui->payloadLineEdit->setTextMargins(0,0,0,0);
//dataGridLayout
ui->dataGridLayout->setAlignment(Qt::AlignLeft);
ui->dataGridLayout->setAlignment(Qt::AlignTop);
for(int i=0; i<4; i++)
{
for(int j=0; j<16; j++)
{
DataLineEdit *lineEdit = new DataLineEdit();
lineEdit->setText("00");//set data default
lineEdit->setInputMask("Hh");//limit to hex values
lineEdit->setFixedSize(20,20);
lineEdit->setTextMargins(0,0,0,0);
lineEdit->setAlignment(Qt::AlignCenter);
lineEdit->setAlignment(Qt::AlignCenter);
connect(lineEdit, SIGNAL(leftRightOverrideKeyPress(int)), this, SLOT(arrowKeyNavigation(int)));
ui->dataGridLayout->addWidget(lineEdit, i, j);
}
}
updateDataGridVisibility();
}
DataWidget::~DataWidget()
{
delete ui;
}
void DataWidget::arrowKeyNavigation(int state)
{
//does something
}
You could edit the class in the following form:
class DataLineEdit: public QLineEdit
{
Q_OBJECT
public:
explicit DataLineEdit(QWidget* parent = 0);
...
and
DataLineEdit::DataLineEdit(QWidget* parent) : QLineEdit(parent)
{
}
Thank you for all the help, it was definitely a linker issue but since I am working within a much larger project, I didn't want to mess with imports or .pro files too much. DataLineEdit is specific to DataWidget, so I simply moved the entire datalineedit.h file into the datawidget.h file at the bottom, and the same for the .cpp files. This avoids any linking all together and keeps things tidier based on the project's file hierarchy.

Segfault on clicking on QFrame

This topic might be little lengthy since I have to explain the premise before procceding with the probleme at hand.Firstly my main goal is to have this application in which the user is capable of drag-n-dropping commands from a toolbar in order to form workflows which are send and executed on a remote server.Currently i am working on the client part in qt and it is driving me nuts.
This is my code:
draglabel.h
#ifndef DRAGLABEL_H
#define DRAGLABEL_H
#include <QLabel>
class QDragEnterEvent;
class QDragMoveEvent;
class QFrame;
class DragLabel : public QLabel
{
public:
DragLabel(const QString &text, QWidget *parent);
QString labelText() const;
private:
QString m_labelText;
};
#endif // DRAGLABEL_H
draglabel.c
#include "draglabel.h"
#include <QtWidgets>
DragLabel::DragLabel(const QString &text, QWidget *parent)
: QLabel(parent)
{
QFontMetrics metric(font());
QSize size = metric.size(Qt::TextSingleLine, text);
QImage image(size.width() + 12, size.height() + 12, QImage::Format_ARGB32_Premultiplied);
image.fill(qRgba(0, 0, 0, 0));
QFont font;
font.setStyleStrategy(QFont::ForceOutline);
QLinearGradient gradient(0, 0, 0, image.height()-1);
gradient.setColorAt(0.0, Qt::white);
gradient.setColorAt(0.2, QColor(200, 200, 255));
gradient.setColorAt(0.8, QColor(200, 200, 255));
gradient.setColorAt(1.0, QColor(127, 127, 200));
QPainter painter;
painter.begin(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(gradient);
painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1),
25, 25, Qt::RelativeSize);
painter.setFont(font);
painter.setBrush(Qt::black);
painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text);
painter.end();
setPixmap(QPixmap::fromImage(image));
m_labelText = text;
}
QString DragLabel::labelText() const
{
return m_labelText;
}
dragwidget.h
#ifndef DRAGWIDGET_H
#define DRAGWIDGET_H
#include <QWidget>
#include <QFrame>
#include <vector>
#include <set>
#include "draglabel.h"
using namespace std;
class QDragEnterEvent;
class QDropEvent;
class DragWidget : public QFrame
{
public:
DragWidget(QWidget *parent = nullptr);
void setMode(int desiredMode);
void changePairingMode();
void showAvailableCommands();
void initDrawingLayout();
vector<tuple<QString,QString>> actCommands;
vector<tuple<QString,QString>> execCommands;
vector<pair<int,int>>waitingForPair;
int pairingMode=0;
QFrame*drawingCon;
private:
int widgetMode=1;
protected:
void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
void dragMoveEvent(QDragMoveEvent *event) Q_DECL_OVERRIDE;
void dropEvent(QDropEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
};
#endif // DRAGWIDGET_H
dragwidget.cpp
#include "draglabel.h"
#include "dragwidget.h"
#include "arrowhead.h"
#include <QtWidgets>
#include <QWidget>
#include <QFrame>
#include <QColor>
#include <tuple>
using namespace std;
static inline QString dndProcMimeType() { return QStringLiteral("application/x-fridgemagnet"); }
DragWidget::DragWidget(QWidget *parent)
: QFrame(parent)
{
drawingCon=new QFrame(this);
QPalette newPalette = palette();
newPalette.setColor(QPalette::Window, Qt::white);
setPalette(newPalette);
setWindowTitle(tr("Drag-and-Drop"));
setMinimumSize(300,300);
setAcceptDrops(true);
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
drawingCon->setPalette(newPalette);
drawingCon->setWindowTitle(tr("Drag-and-Drop"));
drawingCon->setMinimumSize(350,350);
drawingCon->setAcceptDrops(false);
drawingCon->show();
}
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat(dndProcMimeType())) {
if (children().contains(event->source())) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else if (event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}
}
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat(dndProcMimeType())) {
if (children().contains(event->source())) {
if(widgetMode==1)
{
event->setDropAction(Qt::MoveAction);
event->accept();
}
else {
event->ignore();
}
} else {
if(widgetMode==1)
{
event->acceptProposedAction();
}
else
{
if(widgetMode==1)
{
event->accept();
}
else {
event->ignore();
}
}
}
} else if (event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}
}
void DragWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat(dndProcMimeType())) {
const QMimeData *mime = event->mimeData();
QByteArray itemData = mime->data(dndProcMimeType());
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
QString text;
QPoint offset;
dataStream >> text >> offset;
DragLabel *newLabel = new DragLabel(text, this);
newLabel->move(event->pos() - offset);
newLabel->show();
newLabel->setAttribute(Qt::WA_DeleteOnClose);
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
tuple<QString,QString> addTest;
addTest=make_tuple(text,"");
actCommands.push_back(make_tuple(text,""));
for(auto it:actCommands)
qDebug()<<get<0>(it)<<" "<<get<1>(it);
event->acceptProposedAction();
}
} else {if (event->mimeData()->hasText()) {
if(widgetMode==1)
{
event->accept();
}
else {
event->ignore();
}
event->acceptProposedAction();
}
}
}
void DragWidget::mousePressEvent(QMouseEvent *event)
{
DragLabel *child = static_cast<DragLabel*>(childAt(event->pos()));
if(!pairingMode){
if (!child)
return;
QPoint hotSpot = event->pos() - child->pos();
if(widgetMode==1)
qDebug()<<child->labelText();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << child->labelText() << QPoint(hotSpot);
QMimeData *mimeData = new QMimeData;
mimeData->setData(dndProcMimeType(), itemData);
mimeData->setText(child->labelText());
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(*child->pixmap());
drag->setHotSpot(hotSpot);
child->hide();
if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
child->close();
else {
child->show();
}
}
else {
if(widgetMode==1)
{
DragLabel *child = static_cast<DragLabel*>(childAt(event->pos()));
if (!child)
return;
qDebug()<<"Facem pair cu:"<<child->labelText();
waitingForPair.push_back(make_pair(child->x(),child->y()));
if(waitingForPair.size()==2) {
ArrowHead *line=new ArrowHead(waitingForPair.at(0).first,waitingForPair.at(0).second,waitingForPair.at(1).first,waitingForPair.at(1).second,drawingCon);
line->show();
waitingForPair.erase(waitingForPair.begin(),waitingForPair.begin()+1);
qDebug()<<"Tragem linie";
}
}
}
}
void DragWidget::setMode(int desiredMode)
{
widgetMode=desiredMode;
}
void DragWidget::showAvailableCommands()
{
DragLabel*grep=new DragLabel("grep",this);
grep->move(this->x(),this->y());
grep->show();
DragLabel*cat=new DragLabel("cat",this);
grep->move(this->x()+40,this->y());
cat->show();
DragLabel*wc=new DragLabel("wc",this);
wc->move(this->x()+90,this->y());
wc->show();
}
void DragWidget::changePairingMode()
{
if(pairingMode==1)
pairingMode=0;
else {
pairingMode=1;
}
}
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include "dragwidget.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
protected:
virtual void closeEvent(QCloseEvent *event) override;
private slots:
void handleButton();
void closeAppButton();
void pairButton();
private:
QPushButton *executeCode;
QPushButton *pairCommands;
QPushButton *closeApp;
QTextEdit *inputUser;
QTextEdit *outputServer;
DragWidget * commandLayout=new DragWidget();
DragWidget * availableLayout=new DragWidget();
};
#endif // MAINWINDOW_H
mainWindow.cpp
#include "mainwindow.h"
#include "draglabel.h"
#include "dragwidget.h"
#include <QCoreApplication>
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QCloseEvent>
#include <QTextEdit>
#include <QFrame>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
executeCode=new QPushButton("Execute");
closeApp=new QPushButton("Close");
pairCommands=new QPushButton("Pair");
connect(closeApp, SIGNAL (released()), this, SLOT (closeAppButton()));
connect(pairCommands, SIGNAL (released()), this, SLOT (pairButton()));
void pairButton();
QHBoxLayout * horizontalLayout=new QHBoxLayout();
commandLayout->setMode(1);
availableLayout->setMode(2);
horizontalLayout->addWidget(commandLayout);
horizontalLayout->addWidget(availableLayout);
availableLayout->showAvailableCommands();
QVBoxLayout*inputBoxes=new QVBoxLayout();
inputUser=new QTextEdit();
outputServer=new QTextEdit();
inputBoxes->addWidget(inputUser);
inputBoxes->addWidget(outputServer);
horizontalLayout->addLayout(inputBoxes);
QVBoxLayout*withButtons=new QVBoxLayout();
withButtons->addLayout(horizontalLayout);
withButtons->addWidget(pairCommands);
withButtons->addWidget(executeCode);
withButtons->addWidget(closeApp);
withButtons->addWidget(new QFrame());
setCentralWidget(new QWidget);
centralWidget()->setLayout(withButtons);
}
void MainWindow::handleButton()
{
}
void MainWindow::closeEvent(QCloseEvent *event)
{
event->accept();
}
void MainWindow::closeAppButton()
{
exit(EXIT_SUCCESS);
}
void MainWindow::pairButton()
{
commandLayout->changePairingMode();
qDebug()<<commandLayout->pairingMode;
}
Note:It might seem idiotic but i have the same class for the "toolbar",from where you're supposed to drag commands and also for part where you are supposed to drag commands and pair them.
This is mostly modified code of the fridge-magnets example on the qt website.
The problem that is giving headaches is drawing lines between dragwidget, I have tried drawing everything in the same QFrame but that proved to be disastrous since the whole pixelMap of the instance dragWidget is overwritten at every draw.The solution with which i came up is to overlay a supplimentary QFrame over my dragWidget in order to draw lines there and everyone to be happy,but as always misfortune strikes at every step.When i am trying to click on the command widget everything's fine but clicking on anything other than a DragLabel results in a segfault due to clicking on the QFrame due to childAt() returning the address of the QFrame overlayed on the first instance of dragWdiget();
My main question is: How can i overcome this obstacle
You should use qobject_cast instead of static_cast.
Add Q_OBJECT macro in each class declaration:
class DragLabel : public QLabel
{
Q_OBJECT
public:
//... class declaration ...
}
class DragWidget : public QFrame
{
Q_OBJECT
public:
//... class declaration ...
}
Then use qobject_cast instead of static_cast for childAt(), for example :
DragLabel *child = qobject_cast<DragLabel*>(childAt(event->pos()));
if(!child){
....
}

Dynamic QLabel misplacing in UI

I'm trying to create a program that accepts images through drag and drop and shows those images on my UI, then I want to rename them and save them.
I got the drag and drop to work, but I have some issues with my Image placement and I can't seem to find where I'm making my mistake.
In the image you see my UI during runtime, in the top left you can see a part of the image I dragged into the green zone(this is my drag and drop zone that accepts images). The position I actually want it to be in should be the red square. The green zone is a Dynamic created object called Imagehandler that I created to handle the drag and drop of the images. The Red square is my own class that inherits from QLabel, I called it myiconclass. This class should hold the actual image data.
I think my mistake has to do with the layouts, but I can't see it.
Could I get some help with this please?
Imagehandler.h
#ifndef IMAGEHANDLER_H
#define IMAGEHANDLER_H
#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QDrag>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QList>
#include <QDebug>
//this class is designed to help me take in the images with drag and drop
class ImageHandler : public QWidget
{
Q_OBJECT
public:
explicit ImageHandler(QWidget *parent = nullptr);
QList<QImage> getImageListMemory() const;
void setImageListMemory(const QList<QImage> &value);
QList<QUrl> getUrlsMemory() const;
void setUrlsMemory(const QList<QUrl> &value);
private:
//QWidget Icon;
QLabel Icon;
QList <QImage> imageListMemory;
QList <QUrl> urlsMemory;
protected:
void dragEnterEvent(QDragEnterEvent * event);
void dragLeaveEvent(QDragLeaveEvent * event);
void dragMoveEvent(QDragMoveEvent * event);
void dropEvent(QDropEvent * event);
signals:
void transferImageSignal(QList <QImage>);
public slots:
};
#endif // IMAGEHANDLER_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QImageReader>
#include <QList>
#include <QWidget>
#include <QLabel>
#include <myiconclass.h>
#include <imagehandler.h>
#include <QGridLayout>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QList<QImage> getImageListMemory() const;
void setImageListMemory(const QList<QImage> &value);
private:
Ui::MainWindow *ui;
QLabel Icon;
QList <QImage> imageListMemory;
QList <QUrl> urlsMemory;
QList<QWidget *> labelList;
ImageHandler * ImageHandlerMemory;
QGridLayout * grid2;
QList <MyIconClass *> memory;
signals:
public slots:
void setIconSlot(QList <QImage>);
};
#endif // MAINWINDOW_H
myiconclass.h
#ifndef MYICONCLASS_H
#define MYICONCLASS_H
#include <QWidget>
#include <QLabel>
//this class is based on a Qlabel and is only made so it can help me with the actual images, gives me more members if I need it
class MyIconClass : public QLabel
{
Q_OBJECT
public:
explicit MyIconClass(QWidget *parent = nullptr);
int getMyNumber() const;
void setMyNumber(int value);
private:
int myNumber;
signals:
public slots:
};
#endif // MYICONCLASS_H
imagehandler.cpp
#include "imagehandler.h"
ImageHandler::ImageHandler(QWidget *parent) : QWidget(parent)
{
setAcceptDrops(true);
}
QList<QImage> ImageHandler::getImageListMemory() const
{
return imageListMemory;
}
void ImageHandler::setImageListMemory(const QList<QImage> &value)
{
imageListMemory = value;
}
QList<QUrl> ImageHandler::getUrlsMemory() const
{
return urlsMemory;
}
void ImageHandler::setUrlsMemory(const QList<QUrl> &value)
{
urlsMemory = value;
}
void ImageHandler::dragEnterEvent(QDragEnterEvent * event)
{
event->accept();
}
void ImageHandler::dragLeaveEvent(QDragLeaveEvent * event)
{
event->accept();
}
void ImageHandler::dragMoveEvent(QDragMoveEvent * event)
{
event->accept();
}
void ImageHandler::dropEvent(QDropEvent * event)
{
QList <QImage> imageList2;
QList <QUrl> urls;
QList <QUrl>::iterator i;
urls = event->mimeData()->urls();
//imageList.append(event->mimeData()->imageData());
foreach (const QUrl &url, event->mimeData()->urls())
{
QString fileName = url.toLocalFile();
qDebug() << "Dropped file:" << fileName;
qDebug()<<url.toString();
QImage img;
if(img.load(fileName))
{
imageList2.append(img);
}
}
emit transferImageSignal(imageList2);
this->setUrlsMemory(urls);
this->setImageListMemory(imageList2);
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ImageHandler * handler = new ImageHandler(this);
handler->show();
QGridLayout *grid = new QGridLayout;
grid->addWidget(handler, 0, 0);
ui->groupBoxIcon->setLayout(grid);
ImageHandlerMemory = handler;
//connect(handler,SIGNAL(handler->transferImageSignal(QList <QUrl>)),this,SLOT(setIconSlot(QList <QUrl>)));
connect(handler,SIGNAL(transferImageSignal(QList<QImage>)),this,SLOT(setIconSlot(QList<QImage>)));
}
MainWindow::~MainWindow()
{
delete ui;
}
QList<QImage> MainWindow::getImageListMemory() const
{
return imageListMemory;
}
void MainWindow::setImageListMemory(const QList<QImage> &value)
{
imageListMemory = value;
}
void MainWindow::setIconSlot(QList<QImage> images)
{
printf("succes!");
this->setImageListMemory(images); //save the images to memory
QGridLayout *grid = new QGridLayout; //create the grid layout I want my images to be in
// create counters to remember the row and column in the grid
int counterRow =0;
int counterColumn =0;
int counter3 =0;
int counterImages = 0;
//iterate over each image in the list
QList <QImage>::iterator x;
for(x = imageListMemory.begin(); x != imageListMemory.end(); x++)
{
MyIconClass * myLabel = new MyIconClass(this); //create an object of my own class (which is a Qlabel with an int member)
QPixmap pixmap(QPixmap::fromImage(*x)); //create a pixmap from the image in the iteration
myLabel->setPixmap(pixmap); //set the pixmap on my label object
myLabel->show();
memory.append(myLabel); //add it to the memory so I can recal it
counterImages++;
}
while(counter3 < images.count())
{
grid2->addWidget(memory.value(counter3), counterRow, counterColumn);
counterColumn++;
counter3++;
if(counterColumn >= 5)
{
counterRow++;
counterColumn =0;
}
}
if(ImageHandlerMemory->layout() == 0)
{
ImageHandlerMemory->setLayout(grid2);
}
}
myiconclass.cpp
#include "myiconclass.h"
MyIconClass::MyIconClass(QWidget *parent) : QLabel(parent)
{
}
int MyIconClass::getMyNumber() const
{
return myNumber;
}
void MyIconClass::setMyNumber(int value)
{
myNumber = value;
}
As Benjamin T said I had to change this:
MyIconClass * myLabel = new MyIconClass(this);
into this:
MyIconClass * myLabel = new MyIconClass(ImageHandlerMemory);
Thanks Benjamin!
PS, I also had to add this line in my mainwindow constructor:
grid2 = new QGridLayout;

QObject::connect: No such signal progressbarV::keyReleaseEvent()

I am trying to create a project in which I have a progressbarV class which creates a QProgressBar. I am calling this class in my mainWindow. My aim is to navigate to another screen when I click on the progressbar. I tried to implement KeyRleaseEvent for this purpose, but no matter what I do, I keep getting the error "QObject::connect: No such signal progressbarV::keyReleaseEvent()". I would much appreciate any help I could get to resolve this issue.
Please find my code below:-
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QProgressBar>
#include <QLabel>
#include <QPixmap>
#include <QPushButton>
#include <QtWidgets>
#include <QProcess>
#include "headerfiles/progressbarV.h"
#include "headerfiles/redzonesettingsscreen.h"
class progressbarH;
class redZoneSettingsScreen;
class MainWindow : public QMainWindow//,public QProcess
{
Q_OBJECT
private:
progressbarV *progressbar_V_left;
public:
MainWindow();
~MainWindow();
void GetObjects(redZoneSettingsScreen *);
private slots:
void handleSettingsButtonPressed();
/*protected:
virtual void keyReleaseEvent(QKeyEvent *); //Q_DECL_OVERRIDE;*/
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "headerfiles/mainwindow.h"
redZoneSettingsScreen *gotoSettingsScreen;
MainWindow::MainWindow()
{
progressbar_V_left = new progressbarV;
progressbar_V_left->setParent(this);
progressbar_V_left->setGeometry(350,200,90,450);
progressbar_V_left->setTitle("Height");
progressbar_V_left->setData(labelCurHeight->getDataValue());
progressbar_V_left->setMinVal(0);
progressbar_V_left->setMaxVal(labelMaxHeight->getDataValue());
connect(progressbar_V_left, SIGNAL (keyReleaseEvent()), this, SLOT
(handleSettingsButtonPressed()));
}
MainWindow::~MainWindow()
{
delete progressbar_V_left;
}
void MainWindow::GetObjects(redZoneSettingsScreen *button)
{
gotoSettingsScreen = button;
}
void MainWindow::handleSettingsButtonPressed()
{
gotoSettingsScreen->hide();
gotoSettingsScreen->show();
this->hide();
}
/*void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
}*/
progressbarV.h
#ifndef PROGRESSBARV_H
#define PROGRESSBARV_H
#include <QWidget>
#include <QProgressBar>
#include <QLabel>
#include <QPixmap>
class progressbarV: public QWidget
{
Q_OBJECT
private:
QProgressBar *progressbar_V;
QLabel *labelRedDanger, *labelYellowWarning;
float maxScaledHeight, redZoneScaledHeight, yellowZoneScaledHeight;
int spn, spn_value;
QString title;
int data;
short minVal;
short maxVal;
public:
progressbarV();
~progressbarV();
void setSPN(int);
int getSPN();
void setSPN_Value(int);
int getSPN_Value();
void setTitle(QString);
QString getTitle();
void setData(int);
int getData();
void setMinVal(short);
short getMinVal();
void setMaxVal(short);
short getMaxVal();
/*void setLowError(short);
short getLowError();
void setLowWarning(short);
short getLowWarning();
void setHighError(short);
short getHighError();
void setHighWarning(short);
short getHighWarning();*/
QProgressBar* getProgressBarV();
protected:
void keyReleaseEvent(QKeyEvent *); //Q_DECL_OVERRIDE;
};
#endif // PROGRESSBARH_H
progressbarV.cpp
#include "headerfiles/progressbarV.h"
progressbarV::progressbarV()
{
progressbar_V = new QProgressBar;
progressbar_V->setParent(this);
progressbar_V->setStyleSheet("QProgressBar{ border: solid grey; border-
width: 6; border-radius: 12; text-align: center;},
QProgressBar::chunk{background-color: limegreen; width: 0px; margin:
0px;}");
progressbar_V->setGeometry(2,0,50,200);
progressbar_V->setOrientation(Qt::Vertical);
maxScaledHeight = (200*100)/120;
redZoneScaledHeight = 200 - ((maxScaledHeight*105)/100);
yellowZoneScaledHeight = 200 - ((maxScaledHeight*90)/100);
QPixmap mypixRed(":/images/images/redZone.png");
labelRedDanger = new QLabel;
labelRedDanger->setParent(this);
labelRedDanger->setGeometry(8,redZoneScaledHeight,38,3);
labelRedDanger->setPixmap(mypixRed);
QPixmap mypixYellow(":/images/images/yellowZone.png");
labelYellowWarning = new QLabel;
labelYellowWarning->setParent(this);
labelYellowWarning->setGeometry(8,yellowZoneScaledHeight,38,3);
labelYellowWarning->setPixmap(mypixYellow);
}
progressbarV::~progressbarV()
{
delete progressbar_V;
delete labelRedDanger;
delete labelYellowWarning;
}
void progressbarV::setSPN(int val)
{
spn = val;
}
int progressbarV::getSPN()
{
return spn;
}
void progressbarV::setSPN_Value(int val)
{
spn_value = val;
}
int progressbarV::getSPN_Value()
{
return spn_value;
}
void progressbarV::setTitle(QString mTitle)
{
title = mTitle;
}
QString progressbarV::getTitle()
{
return title;
}
void progressbarV::setData(int mData)
{
data = mData;
progressbar_V->setValue(data);
}
int progressbarV::getData()
{
return data;
}
void progressbarV::setMinVal(short mMinVal)
{
minVal = mMinVal;
progressbar_V->setMinimum(minVal);
}
short progressbarV::getMinVal()
{
return minVal;
}
void progressbarV::setMaxVal(short mMaxVal)
{
maxVal = mMaxVal;
progressbar_V->setMaximum(maxVal);
}
short progressbarV::getMaxVal()
{
return maxVal;
}
QProgressBar *progressbarV::getProgressBarV()
{
return progressbar_V;
}
void progressbarV::keyReleaseEvent(QKeyEvent *event)
{
}
Since I am new to QT, kindly give me solutions in the form of code snippets
Thanks in advance,
Sam
you need to declare keyReleaseEvent as a public SLOT
public slots:
void keyReleaseEvent(QKeyEvent *); //Q_DECL_OVERRIDE;
so that QT can connect to it using the old connection style.