i used QT Example : appchooser . i plan to implement the Toolbar with that. i modified it's working fine.
i have problem to catch the click event. i tried but i didn't get the solution. please help me to fix the problem.
for clicking the item i need to call the ItemClicked() method
the project source code.
http://www.4shared.com/file/Xutwi3DR/test4anime.html
Please help to find the solution..
You must subclass it since virtual void grabMouseEvent ( QEvent * event ) (in fact all mouse events) is(are) protected and there are no signals for click events for this widget.
class MyGraphicsWidget : public QGraphicsWidget{
Q_OBJECT
//Implement the constructors as you wish, if you need help with this check a Qt tutorial out.
//to get the mouse events implement the needed functions
//there are many others so just check the docs [1]
virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ){
//do whatever you need here. Emit SIGNALS, show menus, etc
}
};
http://doc.qt.io/archives/qt-4.7/qgraphicswidget-members.html [1]
Related
I just want to disable form's close button while doing a task(by QThread), So I connected the thread's signal "started()" and "finished()" to my two slots, for controlling my form's close button.
//...
m_pTestThread = new TestThread();
connect(m_pTestThread, SIGNAL(started()), this, SLOT(onThreadStart()));
connect(m_pTestThread, SIGNAL(finished()), this, SLOT(onThreadFinish()));
m_pTestThread->start();
//...
void QTest::onThreadStart()
{
this->setWindowFlags(this->windowFlags() & (~Qt::WindowCloseButtonHint));
}
void QTest::onThreadFinish()
{
this->setWindowFlags(this->windowFlags() | Qt::WindowCloseButtonHint);
}
After the thread started, my form was hidden... that is strange.
So I call show() after setWindowFlags() function to avoid this problem,
but still don't know why this happened...
Is this the expected behaviour? Should I call show() after setWindowFlags()?
Check the documentation for setWindowFlags here:
Note: This function calls setParent() when changing the flags for a
window, causing the widget to be hidden. You must call show() to make
the widget visible again..
I am learning qt, and experimenting with examples from a textbook.
The original textbook code has the following, set up to save and close on the x button:
void MainWindow::closeEvent(QCloseEvent *event)
{
if (okToContinue()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}
i experimented with a simple exit in its menu - and it works:
void MainWindow::close()
{
if (okToContinue()) {
QApplication::quit();
}
}
But I want to take advantage of the already written closeEvent, so i replaced the code above with
void MainWindow::close()
{
QCloseEvent *event = new QCloseEvent();
closeEvent(event);
}
I get the checking for changes and saving app, implemented through the okToContinue function. But the application does not close.
i tried to follow through debugging and.. with my small understanding, it seems that there is a close signal being sent...
I don't have a good understanding of this, can somebody please help me figure out what am i doing wrong and how to fix it ?
(the sample code is from C++ GUI Programming with Qt 4, chapter 3)
You don't have to reimplement MainWindow::close() in your subclass.
From the Qt Docs:
...QCloseEvent sent when you call QWidget::close() to close a widget
programmatically...
So you just have to reimplement MainWindow::closeEvent(QCloseEvent *event) if you want to control this event.
This event fires when you click x or call close() from the code.
The closeEvent and related methods don't actually execute the action that happens when a given event is received. They merely allow you to act on the event and perhaps disable its further processing. The closing of the window is done within QWidget::event, where closeEvent is called from.
i have a class implemented from QGraphicsItem, called Node.
i have a rectangle shaped "Node" and i can do something when user clicked on it
i use mousePress and mouseRelease events.
but i want to be notified when user clicked "out of" the rectangle shape.
i tried to implement these functions:
Qt Code:
void Node::focusInEvent ( QFocusEvent * event){
cout<<"in"<<endl;
update();
QGraphicsItem::focusInEvent(event);
}
void Node::focusOutEvent ( QFocusEvent * event ){
cout<<"out"<<endl;
update();
QGraphicsItem::focusOutEvent(event);
}
void Node::hoverEnterEvent(QGraphicsSceneHoverEvent *event){
cout<<"out"<<endl;
}
these do not reacts if i click in or out of rectangle.
should i set a logic on my own for example getting the mouse position and control if it is out of rectangle?
or is there a built in method?
or how can a "Node" object know if other Node object is clicked?
also i wonder, googled but could not found that when does focusinevent and focusoutevent triggered? I guess focusOutEvent must work when i had clicked in the item, then out of the item, am i wrong?
thanks for idea.
You need to do the following when you construct your nodes:
setFlag( QGraphicsItem::ItemIsFocusable );
setAcceptHoverEvents( true );
The first line makes your item actually capable of receiving focus, and the latter makes it so your item is notified of mouse events.
Have you called setFlags method of your graphics item with QGraphicsItem::ItemIsSelectable or QGraphicsItem::ItemIsMovable ?
According to QT doc.
By default, no flags are enabled.
I have a graphicsview and a graphicsscen, but I don't know how to install and handle the event filter for getting the keyboard events. Can anyone help me with that?
Thanks in advance.
If you have created custom QGraphicsScene class you can just override QWidget's "QWidget::keyPressEvent()" and "QWidget::keyReleaseEvent()" methods.
class MyGraphicsScene : QGraphicsScene
{
void keyPressEvent(QKeyEvent *event);
}
//in cpp
void MyGraphicsScene::keyPressEvent(QKeyEvent *event)
{
// do sth with event
}
If you are just using an istance of QGraphicsScene, you can use parent's keyPressEvent. Whether or not you must give more details
You have two options to do that:
1) Create your own class based on QGraphicsView and override keyPressEvent(). That only has sense if you going to change a lot of other things.
2) Install event filter, using installEventFilter(..) method and pass there filter object which will receive everything you might need
In Qt, when a widget receives focus, how can get a notification about it, so I can execute some custom code? Is there a signal or an event for that?
You can add en event filter.
This is an example of an application written with QtCreator. This form has a QComboBox named combobox.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->comboBox->installEventFilter(this);
.
.
.
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::FocusOut)
{
if (object == ui->comboBox)
{
qWarning(object->objectName().toLatin1().data());
}
}
return false;
}
There is a "focusChanged" signal sent when the focus changes, introduced in Qt 4.1.
It has two arguments, he widget losing focus and the one gaining focus:
void QApplication::focusChanged(QWidget * old, QWidget * now)
Qt Designer isn't designed for this level of WYSIWYG programming.
Do it in C++:
class LineEdit : public QLineEdit
{
virtual void focusInEvent( QFocusEvent* )
{}
};
The simplest way is to connect a slot to the QApplication::focusChanged signal.
I'd have to play with it, but just looking at the QT Documentation, there is a "focusInEvent". This is an event handler.
Here's how you find information about.... Open up "QT Assistant". Go to the Index. Put in a "QLineEdit". There is a really useful link called "List of all members, including inherited members" on all the Widget pages. This list is great, because it even has the inherited stuff.
I did a quick search for "Focus" and found all the stuff related to focus for this Widget.
You have hit on of the weird splits in QT, if you look at the documentation focusInEvent is not a slot it is a protected function, you can override it if you are implementing a subclass of your widget. If you you just want to catch the event coming into your widget you can use QObject::installEventFilter it let's you catch any kind of events.
For some odd reason the developers of Trolltech decided to propagate UI events via two avenues, signals/slots and QEvent
Just in case anybody looking for two QMainWindow focus change .
You can use
if(e->type() == QEvent::WindowActivate)
{
//qDebug() << "Focus IN " << obj << e ;
}
QWidget::setFocus() is slot, not signal. You can check if QLineEdit is in focus with focus property. QLineEdit emits signals when text is changed or edited, see documentation.