Drop event will not happen, although `setAcceptDrops' has been called. The following code is based on a widget project created with Qt 5.12.0. After adding in dropEvent() function the cpp file becomes
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug> // added
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true); // added
}
MainWindow::~MainWindow()
{
delete ui;
}
// added; in .h it is in `protected:' section
void MainWindow::dropEvent(QDropEvent *event)
{
qDebug() << "dropEvent";
}
What am I missing? I have been struggling for a few days... Thanks in advance.
You have to overwrite the dragEnterEvent method that allow you to filter by the data type, by the source, by the type of action. In the following example, everything is accepted:
*.h
// ...
protected:
void dropEvent(QDropEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
// ...
*.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true); // added
}
// ...
void MainWindow::dropEvent(QDropEvent *event)
{
qDebug() << "dropEvent" << event;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
For more detail I recommend you read Drag and Drop.
Related
New to C++ and Qt as part of a research project (biology) and have been struggling with presumably some quite simple stuff. I'd really appreciate someone's help.
I'm working with a GUI for a pre-existing programme and I'm trying to transfer a QString variable from the QLineEdit of one of the windows (inputform), to the QLineEdit of a second window (output form).
The bit I'm stuck with is that I need the output form to appear, with it's LineEdit pre-populated, when I click a button on a third window (filedialog).
Problem:
At start up --> two windows appear: filedialog and inputform.
User enters data into inputform's QLineEdit
User presses 'transferButton' on filedialog window
On button press --> outputform appears, with a QLineEdit pre-populated with the user's data (from the inputform).
I assume the problem is of the getter/setter variety and my variable is probably going out of scope, but I've tried following lots of similar examples but can't make it work.
Thanks in advance.
Here's my code:
Main.cpp
#include "filedialog.h"
#include "inputform.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileDialog w;
InputForm w2;
w.show();
w2.show();
return a.exec();
}
filedialog.h
#ifndef FILEDIALOG_H
#define FILEDIALOG_H
#include <QDialog>
namespace Ui {
class FileDialog;
}
class FileDialog : public QDialog
{
Q_OBJECT
public:
explicit FileDialog(QWidget *parent = nullptr);
~FileDialog();
void setFileName();
QString getFileName();
private slots:
void on_transferButton_clicked();
private:
Ui::FileDialog *ui;
QString fileName;
};
#endif // FILEDIALOG_H
filedialog.ccp
#include "filedialog.h"
#include "ui_filedialog.h"
#include "inputform.h"
#include "ui_inputform.h"
#include "outputform.h"
#include "ui_outputform.h"
FileDialog::FileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FileDialog)
{
ui->setupUi(this);
}
FileDialog::~FileDialog()
{
delete ui;
}
void FileDialog::setFileName()
{
InputForm *inputform = new InputForm;
fileName = inputform->ui->inputLineEdit->text();
}
QString FileDialog::getFileName()
{
return fileName;
}
void FileDialog::on_transferButton_clicked()
{
setFileName();
OutPutForm *outputform = new OutPutForm;
outputform->ui->outputLineEdit->setText(getFileName());
outputform->show();
}
inputform.h
#ifndef INPUTFORM_H
#define INPUTFORM_H
#include <QWidget>
namespace Ui {
class InputForm;
}
class InputForm : public QWidget
{
Q_OBJECT
public:
explicit InputForm(QWidget *parent = nullptr);
~InputForm();
Ui::InputForm *ui;
};
#endif // INPUTFORM_H
inputform.ccp
#include "inputform.h"
#include "ui_inputform.h"
InputForm::InputForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputForm)
{
ui->setupUi(this);
}
InputForm::~InputForm()
{
delete ui;
}
outputform.h
#ifndef OUTPUTFORM_H
#define OUTPUTFORM_H
#include <QWidget>
namespace Ui {
class OutPutForm;
}
class OutPutForm : public QWidget
{
Q_OBJECT
public:
explicit OutPutForm(QWidget *parent = nullptr);
~OutPutForm();
Ui::OutPutForm *ui;
};
#endif // OUTPUTFORM_H
outputform.ccp
#include "outputform.h"
#include "ui_outputform.h"
OutPutForm::OutPutForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::OutPutForm)
{
ui->setupUi(this);
}
OutPutForm::~OutPutForm()
{
delete ui;
}
Thank you for your brief pointer.
After some playing around:
Setup mainwindow (or in my case main dialog window). Generate inputform instance, connect button to inputform.
FileDialog::FileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FileDialog)
{
ui->setupUi(this);
InputForm *inputForm = new InputForm;
connect(ui->transferButton,SIGNAL(clicked()),inputForm,SLOT(getLineEditTextFunc()));
inputForm->show();
}
FileDialog::~FileDialog()
{
delete ui;
}
void FileDialog::on_transferButton_clicked()
{
}
Then from the input form:
Define a function to get the input form's LineEdit text (fileName); and then also generate an output form and populate it's LineEdit with the fileName variable.
InputForm::InputForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputForm)
{
ui->setupUi(this);
}
InputForm::~InputForm()
{
delete ui;
}
void InputForm::getLineEditTextFunc()
{
fileName = this->ui->inputLineEdit->text();
OutPutForm *outputform = new OutPutForm;
outputform->ui->outputLineEdit->setText(fileName);
outputform->show();
}
I have a QListView named listView. It is the only widget in the MainWindow. I want to track the double clicks on the listView. So, I did this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
listView = new QListView(this);
this->setCentralWidget(listView);
connect(listView, &QListView::doubleClicked, this, &MainWindow::onDoubleClicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow :: onDoubleClicked(const QModelIndex &index)
{
QMessageBox :: information(this, "Info", "List view was double clicked at\nColumn: " + QString :: number(index.column()) + " and Row: " + QString::number(index.row()));
}
But when I double click the listView a get no message box
If the docs are reviewed:
void QAbstractItemView::doubleClicked(const QModelIndex &index)
This signal is emitted when a mouse button is double-clicked. The item
the mouse was double-clicked on is specified by index. The signal is
only emitted when the index is valid.
In your case, your QListView does not have a model, so when you click there is no valid QModelIndex, so the signal will not be emitted.
If you want to follow the double-click event there are 2 possible solutions:
Create a QListView and overwrite the mouseDoubleClickEvent event.
Or use an event filter.
In my solution I will use the second method:
*.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QListView;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool eventFilter(QObject *watched, QEvent *event);
private:
Ui::MainWindow *ui;
QListView *listView;
};
#endif // MAINWINDOW_H
*.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QEvent>
#include <QListView>
#include <QMouseEvent>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
listView = new QListView;
this->setCentralWidget(listView);
listView->viewport()->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(watched == listView->viewport() && event->type() == QEvent::MouseButtonDblClick){
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
qDebug()<<"MouseButtonDblClick"<<mouseEvent->pos();
}
return QMainWindow::eventFilter(watched, event);
}
I need to perform an operation when the size of a widget (MyForm) changes.
Is there a signal which is emitted when the size of a widget changes? (I could not find it in the documentation).
I need to do something like this:
#include "myform.h"
#include "ui_myform.h"
MyForm::MyForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyForm)
{
ui->setupUi(this);
connect(this, SIGNAL(sizeChanged()), this, SLOT(refresh()));
}
MyForm::~MyForm()
{
delete ui;
}
void MyForm::refresh()
{
ui->label->setText( QString::number(this->width()) + ", " + QString::number(this->height()));
}
When the widget size changes, it 'calls' the refresh function which updates a label with the current width and height. Note: it is only an example which can be easily reproduced.
Sure I can use a QTimer, for example:
#include "myform.h"
#include "ui_myform.h"
MyForm::MyForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyForm)
{
ui->setupUi(this);
timer_ = new QTimer(this);
connect(timer_, SIGNAL(timeout()), this, SLOT(refresh()));
timer_->start(100);
}
MyForm::~MyForm()
{
delete ui;
}
void MyForm::refresh()
{
ui->label->setText( QString::number(this->width()) + ", " + QString::number(this->height()));
}
But I don't think it is the best solution.
Note: I'm using Qt 5.3.
It is not necessary to create a signal, you just have to overwrite resizeEvent() and call refresh from that method:
*.h
protected:
void resizeEvent(QResizeEvent *event);
*.cpp
void MyForm::resizeEvent(QResizeEvent *event)
{
refresh();
QWidget::resizeEvent(event);
}
I have written a small program to send data from one form(MainWindow) to another(Dialog) upon a button click. When the button is clicked the value written in the lineEdit of MainWindow is to be displayed on a label in Dialog form!
When I click the button a value is displayed on the label but it is not the same as the value entered in the line edit!
following are the respective codes in the 2 header and 2 cpp files!
MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
signals:
void sendIntData(int data);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
}
MainWIndow.cpp
void MainWindow::on_pushButton_clicked()
{
Dialog *dialog1=new Dialog(this);
dialog1->setModal(true);
dialog1->exec();
int o=ui->lineEdit->text().toInt();
connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));
emit sendIntData(o);
}
Dialog.h
class Dialog : public QDialog
{
Q_OBJECT
public slots:
void setIntData(int data);
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
}
Dialog.cpp
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::DIalog)
{
ui->setupUi(this);
QString value=QString::number(index);
ui->label->setText(value);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::setIntData(int data)
{
index=data;
}
eg-When I click 3 and press the button I get a value 7237481! How can I correct this?
Replace connect and emit in on_pushButton_clicked()
void MainWindow::on_pushButton_clicked()
{
Dialog *dialog1=new Dialog(this);
dialog1->setModal(true);
dialog1->exec();
int o=ui->lineEdit->text().toInt();
connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));
emit sendIntData(o);
}
If only once we convey our dialogue, the importance of signal and slot is not necessary.
It is possible to give this value to the constructor or to do the initialize function and to give it the values.
//way 1:
void MainWindow::on_pushButton_clicked(){
Dialog *dlg = new Dialog();
connect(this, SIGNAL(SendData(int)), dlg, SLOT(slotData(int)));
emit SendData(ui->lineEdit->text().toInt());
dlg->exec();
}
void Dialog::slotData(int arg1)
{
ui->label->setText(QString::number(arg1));
}
//way 2:
void MainWindow::on_pushButton_clicked(){
Dialog* dlg = new Dialog(ui->lineEdit->text().toInt());
dlg->exec();
}
//way 3:
#include "dialog.h"
#include "ui_dialog.h"
#include "QDebug"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::initialize(int value)
{
ui->label->setText(QString::number(value));
}
void MainWindow::on_pushButton_clicked(){
Dialog *dlg = new Dialog();
dlg->initialize(ui->lineEdit->text().toInt());
dlg->exec();
}
I think you are showing int value which not initialized.
emit signal:
int o=ui->lineEdit->text().toInt();
connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));
emit sendIntData(o);
Show value:
void Dialog::setIntData(int data)
{
ui->label->setText(QString::number(data));
}
both events work properly on bare mainWindow but when I press inside the graphicsView ,placed inside the mainWindow, only mousePressEvent is responding.
could anybody clarify this issue?
UPD: Here is the code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
pix = new QPixmap("/Users/mac/Pictures/wallpaper/Rocks.jpg");
scene->addPixmap(*pix);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
sel_reg_beg_x = e->x();
sel_reg_beg_y = e->y();
qDebug() << "inside press";
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << "inside move";
sel_reg_end_x = e->x();
sel_reg_end_y = e->y();
this->update();
}
You have two options here:
Derive your own graphics view from the QGraphicsView and implement the mouse move event handler there.
Create the event filter and install it into your QGraphicsView's viewport (ui->graphicsView->viewport()->installEventFilter(...)). See the QObject::eventFilter() documentation.
And of course, you have to enable mouse tracking also for the QGraphicsView's viewport:
ui->graphicsView->viewport()->setMouseTracking(true);