Pretty much as the tittle says, when i drag an item from QTableWidget it adds a child to ui->table->children, and then it launches ChildAdded event to my eventFilter, the problem is that if i drop the dragged item into an area that doesn't accept dropping that child won't be removed from ui->table->children whereas releasing the dragged item within an area that has dropping enabled it will, and also launches ChildRemoved event.
Here is my init code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_ui(new Ui::MainWindow)
{
m_ui->setupUi(this);
m_ui->left_table->installEventFilter(this);
m_ui->right_table->installEventFilter(this);
m_ui->left_table->setColumnCount(1);
m_ui->left_table->setRowCount(25);
m_ui->left_table->setHorizontalHeaderLabels({"Source"});
m_ui->left_table->horizontalHeader()->setStretchLastSection(true);
m_ui->left_table->setAcceptDrops(false);
m_ui->left_table->setDragEnabled(true);
m_ui->right_table->setColumnCount(1);
m_ui->right_table->setHorizontalHeaderLabels({"Receiver"});
m_ui->right_table->horizontalHeader()->setStretchLastSection(true);
m_ui->right_table->setAcceptDrops(true);
for (int i = 0; i < 25; ++i) {
m_ui->left_table->setItem(i, 0, new QTableWidgetItem(QString::number(i)));
}
qDebug() << "Left childs: " << m_ui->left_table->children().size();
}
and the event filter:
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::ChildAdded || event->type() == QEvent::ChildRemoved) {
qDebug() << "Object: " << watched->objectName()
<< " Event: " << event->type()
<< "Childs: " << m_ui->left_table->children().size();
}
return false;
}
Example of dropping on invalid area, this wont remove the child added before:
Example of dropping on valid area, this will remove the child and call the ChildRemoved event:
Related
I have a qt and cpp application that creates threads on create button and runs them with a certain start time specified by the user. It list the thread object created into a QListWidget in the application with some random id generated. I can select and delete the QListWidget item as shown below.
void MainWindow::on_stopPushButton_clicked()
{
qDebug() << mythread->threadIdGenerator();
QList<QListWidgetItem*> items = ui->threadWithId->selectedItems();
foreach(QListWidgetItem* item, items){
ui->threadWithId->removeItemWidget(item);
delete item; // Qt documentation warnings you to destroy item to effectively remove it from QListWidget.
}
}
This delete operation just deletes the entry from the UI (QListWidget) of the application but the thread deleted from QListWidget is still running in the application.
My question is how can I get terminate this thread with deletion from the QListWidget from the UI of the application?
Or is there any way to get this thread object and delete it after being selected from the UI.
Update
void MainWindow::on_createNewThreadButton_clicked()
{
qDebug(" in create new thread slot");
mythread = new MyThread(this);
mythread->threadOccuranceTime = ui->insertThreadTimeoutHere->text().toInt();
mythread->start();
myThreadListCreatedObjects.append(mythread);
connect(mythread, SIGNAL(signalForThreadMessage(int)), this, SLOT(displayThreadMessages(int)));
connect(mythread, SIGNAL(sendThreadId(int)), this, SLOT(displayThread(int)));
}
MyThread.cpp
int MyThread::threadIdGenerator()
{
qDebug(" in threadIdGenerator function");
// qDebug() << 1 + (rand() % 100);
srand(time(0));
return 1 + (rand() % 100);
}
void MyThread::run()
{
this->threadId = 0;
qDebug() << this->threadId;
qDebug(" in run function for the thread");
this->threadId = this->threadIdGenerator();
qDebug(" id inside the run method of thread");
qDebug() << threadId;
emit sendThreadId(threadId);
while (1) {
msleep(this->threadOccuranceTime);
emit signalForThreadMessage(this->threadId);
}
}
I used QList to store all the threads created, and when a item is selected in the QLIstWidget I tried to get the thread with that id property, terminate it and then remove from the QListWidget from UI as shown below.
void MainWindow::on_stopPushButton_clicked()
{
QList<QListWidgetItem*> items = ui->threadWithId->selectedItems();
foreach(QListWidgetItem* item, items){
qDebug() << "the item selected to delete";
qDebug() << item->text();
ui->threadWithId->removeItemWidget(item);
QString selected = item->text();
QString subString = selected.mid(0,2);
qDebug() << "subString = ";
qDebug() << subString;
for(MyThread *sp:myThreadListCreatedObjects){
qDebug() << "thread id = ";
qDebug() << sp->threadId;
if(sp->threadId == subString.toInt()){
qDebug() << "subString = ";
qDebug() << subString;
sp->terminate();
delete sp;
}
}
delete item; // Qt documentation warnings you to destroy item to effectively remove it from QListWidget.
}
}
I am not sure this is the correct way of doing the things but it works some how. Any better solution is welcome :).
I expect my program to print "mouse on label name" when my mouse is located on the labelname (a QLabel), and to print"mouse not on label name" when my mouse is not located on the labelname.
Even though I put my mouse on labelname, my program prints "mouse not on label name".
How can I know when my mouse is not on the labelname?
bool Dialog::eventFilter(QObject *obj, QEvent *e)
{
if(qobject_cast<QLabel*>(obj) == ui->labelname) {
cout << “mouse on label name” << endl;
}else if(qobject_cast<QLabel*>(obj) != ui->labelname) {
cout << “mouse not on label name” << endl;
}
return false;
}
Be sure you are installing the event filter correctly. Also, if you want to track mouse position you have to enable mouseTracking, otherwise the move events won't be triggered (though QEvent::Enter and QEvent::Leave will, which are the ones that indicates the mouse has entered or left the widget).
Here a minimal example of how to do it:
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
m_label = new QLabel("Hello world!");
m_label->setObjectName("m_label");
m_label->installEventFilter(this);
m_label->setMouseTracking(true);
auto hlayout = new QVBoxLayout();
hlayout->addWidget(m_label);
setLayout(hlayout);
}
bool MyWidget::eventFilter(QObject* sender, QEvent* event)
{
if (sender == m_label) {
qDebug() << sender->objectName() << event->type();
if (event->type() == QEvent::Enter) {
qDebug() << "mouse on label name";
} else if (event->type() == QEvent::Leave) {
qDebug() << "mouse not on label name";
}
}
return QWidget::eventFilter(sender, event);
}
The full working example is available in GitHub.
Hi I need to draw a rectangle over Qlabel, as a first step I am trying to get the mouse events. Using below code the event get called but the co-ordinates are always same even if I changed the mouse position.
Also I need to set the mouse tracking only on the Qlabel, and it's working fine except the mouse release, the function mouseReleaseEvent getting called when the mouse released outside the Qlabel.
Also please see the comments in the below code,
ImageEditer::ImageEditer(QWidget *parent) :
QDialog(parent),
ui(new Ui::ImageEditer)
{
ui->setupUi(this);
ui->label_image->setMouseTracking(true); // need to set the mouse tracking over this label
}
void ImageEditer::mouseMoveEvent(QMouseEvent* event)
{
qDebug() << "Mouse move.." << pos() << "---> " << x() << "," << y(); // these printing same values
}
void ImageEditer::mousePressEvent(QMouseEvent* event)
{
qDebug() << "Mouse press.." << pos() << "---> " << x() << "," << y(); // these printing same values
}
// This getting called when release the mouse outside the label_image
void ImageEditer::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << "Mouse release.." << pos() << "---> " << x() << "," << y(); // these printing same values
}
You are printing pos(), x(), y() of the widget.
Just change it to event->pos(), event->pos().x(), event->pos().y()
I was wondering if there was a way I could remove all keyboard shortcuts from the UI so I can use the event filter to grab key commands instead.
Currently, I have this in my eventFilter() function and it does the job but it also affects the QSliders, QLineEdits and QButtons I have on there - and I don't want it to.
bool LinkControl::eventFilter(QObject *o, QEvent *e)
{
QKeyEvent *keyEvent = NULL;
// qDebug() << "TYPE: " << e->type();
switch(e->type())
{
case 51:
// qDebug() << this->overrideControl;
if(this->overrideControl == true)
{
keyEvent = static_cast<QKeyEvent*>(e);
// this->handleKeyboardInput(true, keyEvent->key());
// emit handleInput(true, keyEvent->key());
this->handleKeyboardInput(true, keyEvent->key());
// qDebug() << "PRESS: " << keyEvent->key();
}
this->overrideControl = !this->overrideControl;
return true;
break;
case QEvent::KeyRelease:
keyEvent = static_cast<QKeyEvent*>(e);
// this->handleKeyboardInput(false, keyEvent->key());
// emit handleInput(false, keyEvent->key());
this->handleKeyboardInput(false, keyEvent->key());
// qDebug() << "RELEASE: " << keyEvent->key();
return true;
break;
default:
return QMainWindow::eventFilter(o, e);
}
}
For example, In my handleKeyboardInput function, I have the up arrow key increment a value and it does that but it also moves a QSlider if it's selected.
Thank you.
I am writing a keyboard tester, so I start a dialog with
keybDialog keyboardTestWindow;
keyboardTestWindow.setWindowTitle("Keyboard test");
keyboardTestWindow.exec();
override the keyPressEvent(QKeyEvent *event) and the test works superb. Now, I needed use of some parameters from parent, so instead I did
keybDialog keyboardTestWindow(m_parent);
keyboardTestWindow.setWindowTitle("Keyboard test");
keyboardTestWindow.exec();
and implemented what I needed. Now I discovered that just by instantiating the class and passing the parent to it, my dialog no longer receives the keyPressEvent. Are these passed to m_parent in some way? How can I avoid this?
EDIT:
The beginning of my keyPressEvent:
void keybDialog::keyPressEvent(QKeyEvent *event)
{
ui->txtBxKeyboardInput->clear();
qDebug() << "Event: " << hex << event->key() << event->nativeVirtualKey() << event->modifiers() << event->nativeModifiers() << event->nativeScanCode();
ui->txtBxKeyboardInput->setText(ui->txtBxKeyboardInput->text()+event->text());
switch(sequenceNumber)
{
case 0: // Left Shift + R
if( (event->key() == Qt::Key_R) && event->modifiers() == Qt::ShiftModifier && event->nativeModifiers() == 0x201)
{
reportSuccessfullKey(sequenceNumber);
}
else
{
ui->lblStatus->setText("WRONG");
}
break;