Recursive repaint detected in popup window - qt

I have a QPushButton that will open new window on clicked.
void showNewWindow()
{
PopupWindow *popup = new PopupWindow();
popup->show();
}
And PopupWindow is declared as following:
class PopupWindow : public QWidget {
Q_OBJECT
public:
PopupWindow(QWidget* parent);
void setContent(QString content) { this->content = content; }
QString getContent() { return content; }
private:
QString content;
private slots:
void handleContinue();
void handleRunToEnd();
};
Then I implement a its constructor:
PopupWindow::PopupWindow(QWidget *parent):QWidget(parent)
{
QHBoxLayout *hlayout = new QHBoxLayout();
QWidget *buttonWidget = new QWidget();
QPushButton *btnContinue = new QPushButton();
btnContinue->setText("Continue");
QPushButton *btnRunEnd = new QPushButton();
btnRunEnd->setText("Run till completion");
buttonWidget->setLayout(hlayout);
hlayout->addWidget(btnContinue);
hlayout->addWidget(btnRunEnd);
connect(btnContinue,SIGNAL(clicked()), this, SLOT(handleContinue()));
connect(btnRunEnd,SIGNAL(clicked()), this, SLOT(handleRunToEnd()));
QTextEdit *html = new QTextEdit();
html->setReadOnly(true);
html->setText("AAAA");
QVBoxLayout *layout = new QVBoxLayout();
this->setLayout(layout);
layout->addWidget(html);
layout->addWidget(buttonWidget);
}
My problem: whenever I click on the "Continue" or "Run till completion" buttons on Popup Window. The app crashed. I could see the error as following:
QApplication: Object event filter cannot be in a different thread.
QApplication: Object event filter cannot be in a different thread.
QApplication: Object event filter cannot be in a different thread.
QWidget::repaint: Recursive repaint detected
Please, help me to resolve this.

Related

How to Delete Item from Graphics scene from Main Window Tool Box?

In Graphic View set the scene, In Graphic scene(subclass od QGraphicscene) class added Delete item slot.In scene class by delete key i able to delete item but when i call from main window it wont delete item . i am getting call in Delete item slot but selectedItems = 0. what may be causing problem?
In Graphic scene class
void GraphicScene::DeleteItems()//Delete Item slot in scene class
{
qDebug()<<"delete items"<< selectedItems().count();
foreach(QGraphicsItem* item, selectedItems())
{
removeItem(item);
delete item;
}
}
void GraphicScene::keyReleaseEvent(QKeyEvent * keyEvent)// Delete key works fine
{
if (selectedItems().isEmpty())
return;
if(keyEvent->key() == Qt::Key_Delete)
{
DeleteItems();
}
}
In MainWindow class
MainWindow::MainWindow(QWidget *parent)
{
addToolBar(Qt::TopToolBarArea, mpEditToolbar = new
QToolBar());
DeleteAction = new QAction(QIcon(":/images/delete.png"),tr("Object
&Delete"), this);
DeleteAction->setStatusTip(tr("Delete item"));
connect(DeleteAction,SIGNAL(triggered()),mpGraphView ,
SIGNAL(DeleteObject())); // grpah view connecting to delete slot
mpEditToolbar->addAction(DeleteAction);
}
When i do from delete key works fine its not working with tool box delete action. what is the problem?
In Main Window class have private members of GraphicsView and GraphicScene class(subclass)
so that it will be easy to call slot.
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
GraphFrame *mpGraphFrame;
GraphicScene *mpScene;
}
MainWindow.cpp
Connect should be in where you creating GraphicScene Object
MainWindow::MainWindow(QWidget *parent)
{
addToolBar(Qt::TopToolBarArea, mpEditToolbar = new
QToolBar());
DeleteAction = new QAction(QIcon(":/images/delete.png"),tr("Object
&Delete"), this);
DeleteAction->setStatusTip(tr("Delete item"));
connect(DeleteAction,SIGNAL(triggered()),mpGraphScene ,
DeleteItems();
mpEditToolbar->addAction(DeleteAction);
}

Create and specify a QLabel after mouse pressed on another QLabel in QT

Ok so what i'm trying to do is to create a new QLabel added to a QList and put it where I clicked on the other QLabel where I clicked.
So here is my code:
class CustomLabel : public QLabel
{
Q_OBJECT
public:
CustomLabel();
void mousePressEvent( QMouseEvent* event);
private:
QList<QLabel *> pointsL;
QList<QPoint *> points;
};
void CustomLabel::mousePressEvent(QMouseEvent *event)
{
points << new QPoint(event->pos());
pointsL << new QLabel(this);
pointsL.at(pointsL.size()-1)->setText("+");
pointsL.at(pointsL.size()-1)->setGeometry(QRect(points.at(points.size()-1)->rx(),, points.at(points.size()-1)->ry(), 1, 1));
}
I also tried:
pointsL.at(pointsL.size()-1)->move(points.at(points.size()-1)->rx(), points.at(points.size()-1)->ry());
and this:
void CustomLabel::mousePressEvent(QMouseEvent *event)
{
points << new QPoint(event->pos());
pointsL << new QLabel(this);
pointsL.at(pointsL.size()-1)->setText("+");
pointsL.at(pointsL.size()-1)->move(*points.at(points.size()-1));
pointsL.at(pointsL.size()-1)->setTabOrder(pointsL.at(pointsL.size()-1), this);
}
When I click on the Custom Label nothing happens. The constructor is empty.
Thanks for any answer.
New widgets added after the parent is already visible on screen should be shown explicitly unless they are in a layout.
So basically you should add:
pointsL.back()−>show();

How to create a draggable (borderless and titleless) top level window in QT

I'd appreciate help creating a top-level window in Qt with the following characteristics. The window must be:
Borderless, titleless and lie on top of all other windows on the desktop (easy)
Draggable by clicking and dragging anywhere inside it (this what I need help with)
Constrained to the top border of the desktop while dragging (relatively easy)
Basically, I'm trying to collapse our QT application to a top-level icon on the top border of the desktop.
You'll find the answer to the first part in: Making a borderless window with for Qt, and the answer to the second part in Select & moving Qwidget in the screen.
Combining the two, and adding the last part is straightforward.
Here's how you could do it:
#include <QtGui>
class W: public QWidget
{
Q_OBJECT
Set up a borderless widget with a few buttons to lock/unlock and quit:
public:
W(QWidget *parent=0)
: QWidget(parent, Qt::FramelessWindowHint), locked(false)
{
QPushButton *lock = new QPushButton("Lock");
QPushButton *unlock = new QPushButton("Unlock");
QPushButton *quit = new QPushButton("&Quit");
connect(lock, SIGNAL(clicked()), this, SLOT(lock()));
connect(unlock, SIGNAL(clicked()), this, SLOT(unlock()));
connect(quit, SIGNAL(clicked()),
QApplication::instance(), SLOT(quit()));
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(lock);
l->addWidget(unlock);
l->addWidget(quit);
setLayout(l);
}
public slots:
void lock() {
locked = true;
move(x(), 0); // move window to the top of the screen
}
void unlock() { locked = false; }
Do the mouse handling:
protected:
void mousePressEvent(QMouseEvent *evt)
{
oldPos = evt->globalPos();
}
void mouseMoveEvent(QMouseEvent *evt)
{
const QPoint delta = evt->globalPos() - oldPos;
if (locked)
// if locked, ignore delta on y axis, stay at the top
move(x()+delta.x(), y());
else
move(x()+delta.x(), y()+delta.y());
oldPos = evt->globalPos();
}
private:
bool locked;
QPoint oldPos;
};

keep Focus on QGLWidget with QCheckBox and QPushButton in MainWindow

I have a Qt main window with QCheckBox and QPushButton and a sub QGLwidget class widget for graphics rendering.
I have put into void Ui_MainWindow::setupUi(QMainWindow *MainWindow) member function :
void Ui_MainWindow::setupUi(QMainWindow *MainWindow)
{
pushButton_2 = new QPushButton(widget);
...
checkBox_3 = new QCheckBox(widget);
...
widget_2 = new GLWidget(widget);
widget_2->setFocusPolicy(Qt::StrongFocus);
widget_2->setFocus();
...
}
I have created signals which modify the graphics rendering of widget_2 :
void Ui_MainWindow::createSignals()
{
...
connect(pushButton_2, SIGNAL(clicked()), this, SLOT(pauseSimu()));
connect(checkBox_3, SIGNAL(clicked()), this, SLOT(hideClassic()));
...
}
To always keep the focus on widget_2 despite clicking on pushButton_2 or checkBox_3, I have to put into pauseSimu() and hideClassic() :
void Ui_MainWindow::pauseSimu()
{
widget_2->setFocus();
...
}
and
void Ui_MainWindow::hideClassic()
{
widget_2->setFocus();
...
}
The key events on widget_2 GLWidget are coded in the GLWidget class member functions.
How could I avoid to use setFocus() in all signals functions for always keeping the focus on widget_2 GLWidget ?
Try calling setFocusPolicy(Qt::NoFocus) on your button and checkbox.

QDialog with floating toolbar

How can I create a QDialog with floating toolbar in Qt?
Attachment of the QMainWindow with toolbar as widget in the QDialog is not suitable.
Why not suitable? following code works like charms.
#include <QtGui>
class MyDialog : public QDialog
{
Q_OBJECT
public:
MyDialog(QWidget* parent=0)
{
QMainWindow* child = new QMainWindow;
QLabel* label = new QLabel(tr("QMainWindow with toolbar!"));
label->setAlignment(Qt::AlignCenter);
child->setCentralWidget(label);
QToolBar* toolbar = child->addToolBar(tr("Tool"));
toolbar->addAction(tr("Test"), this, SLOT(doTest()));
QHBoxLayout* layout = new QHBoxLayout(this);
layout->setContentsMargins(0,0,0,0);
layout->addWidget(child);
}
private slots:
void doTest()
{
QMessageBox::information(this, tr("Test"), tr("ToolBar is Working!"));
}
};
look at
Can you add a toolbar to QDialog?
and try to write smth like that
myDialog->layout()->setMenuBar(myToolBar);

Resources