Hiding a QWidget on a QToolbar? - c++

I have directly added some QWidgets to a QToolbar but simply going widget->setVisible(false) did not work. Can someone please give me an example of how to show and hide a widget that is on a QToolbar?
Thanks!

You need to call setVisible() on the appropriate QAction instead. For example, addWidget() returns a QAction*:
QAction* widgetAction = toolBar->addWidget(someWidget);
widgetAction->setVisible(false);

Related

qt delegate editor pops up with no parent

I'm using a QStyledItemDelegate in QTableView. I followed tutorial http://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html . No problem for this.
I just changed QSpinBox to a custom widget I designed (let's call it MyWidget). It's also working, but now, whenever I trigger a cell of QTableView, editor MyWidget is poping up as a parentless widget would do. I've been trying to understand what makes the editor appear at cell's location but without any success.
In short : I'd like MyWidget to appear at cell's location and be part of QTableView. As QSpinBox does in tutorial example. Is it possible ? If so, how can I achieve this ? Thanks
Edit
Solved : parent argument routing from MyWidget to QWidget was wrong.
Solved : parent argument routing from MyWidget to QWidget was wrong.

How to add QLabel to QGraphicsItem

I have a QGraphicsItem that has text on it. I want this text to be editable, so that if the user double-clicks it, it will enter an edit mode. It seems like the easiest way to do this would be to change the text into a QLineEdit and let the user click away the focus or press enter when they're done.
How can I add a QLineEdit to a QGraphicsItem? I have subclassed the QGraphicsItem so I have access to its internals.
To add any QWidget based object to a QGraphicsScene, a QGraphicsProxyWidget is required.
When you call the function addWidget on QGraphicsScene, it embeds the widget in a QGraphicsProxyWidget and returns that QGraphicsProxyWidget back to the caller.
The QGraphicsProxyWidget forwards events to its widget and handles conversion between the different coordinate systems.
Now that you're looking at using a QLineEdit in the QGraphicsScene, you need to decide if you want to add it directly:
QGraphicsScene* pScene = new QGraphicsScene;
QLineEdit* pLineEdit = new QLineEdit("Some Text");
// add the widget - internally, the QGraphicsProxyWidget is created and returned
QGraphicsProxyWidget* pProxyWidget = pScene->AddWidget(pLineEdit);
Or just add it to your current QGraphicsItem.
Here, you can either add it as a child of the QGraphicsItem:
MyQGraphicsItem* pMyItem = new MyQGraphicsItem;
QGraphicsProxyWidget* pMyProxy = new QGraphicsProxyWidget(pMyItem); // the proxy's parent is pMyItem
pMyProxy->setWidget(pLineEdit); // adding the QWidget based object to the proxy
Or you could add the QGraphicsProxyWidget as a member of your class and call its relevant functions, but adding it as a child is probably much simpler.
QGraphicsTextItem::setTextInteractionFlags (Qt::TextInteractionFlags flags)
API can be used. But you need to create QGraphicsTextItem inside it.
Please check following link for details: Implementation details
You need to create a proxy widget by extending QGraphicsProxyWidget in the case you need some specific behavior or just use a QGraphicsProxyWidget. Take a look at the "Embedded Dialogs" example in your Qt SDK and the QGraphicsProxyWidget documentation. It has been there for a long time so it should be for your version. I hope this helps.

Change text in a QAction that's inside of a QMenu

I have a QMenu objects to which I've added several QAction objects with a certain text. All works well, except that I would like to change the text of those QActions at runtime. I've looked at the QMenu API and didn't find any way to to get them. I also tried actions.at(x) and a few other things. What's the right way to do this?
It really depends on how you are structuring your app. In some circumstances you might be saving your QAction's as members, to which you could access directly. Or you might just be saving the QMenu and populating it with QAction's. Either way, once you have your action, you just call setText(QString) on it:
// init
menu = new QMenu(this);
...
menu->addAction("foo");
// later on
QAction *action = menu->actions().at(0); // access just the first QAction
action->setText("bar");
QMenu also allows you to look up actions by a QPoint location. Again, it really depends on exactly how you will be determining which action you want to change.

Qt -how to know whether content in child widgets has been changed?

In QMainWindow I have 2 QSplitters. In that splitters I have QTextEdit, QLineEdits, QTableWinget, Ragio buttons and so on... I want to know if somthing has been chaged after pressing File->New menu button. Is there any general method for doing this?
Somwhere I have read that it is recomended to use isWindowModified() function of QMainWindow, but seems it doesn't work.
setWindowModified() does not propagate the windowModified flag to the parents. This bug is described here: https://bugreports.qt.io/browse/QTBUG-20150. I have just tried it and indeed it did not work.
The isWindowModified() could be useful here since according to http://doc.trolltech.com/4.6/qwidget.html#windowModified-prop it propagates up to the parent.
However, I think you would need to set this yourself. For example, if you clicked the new button which leads to some text being inserted into a QTextEdit, you still need to call QTextEdit's setWindowModified() function - which will then propagate up to your QMainWindow - and you can just check QMainWindow afterwards. (However, you wouldn't know which children were modified)
Maybe you should have a look at QWidget::changeEvent.

Problem with event handling on QToolButton in Linux

I am developing an application, I have added a QToolBar object in that, and have added the QToolButton object on that, I have also connect the clicked() event with that but the problem is that the mouse click event don't work on QToolButton but when I bring focus on that using Tab, then space button works fine, but I want it with mouse click.. any idea? here is the code.
pToolBar = new QToolBar(this);
pToolBar->setAllowedAreas(Qt::NoToolBarArea);//NoToolBarAreaAllToolBarAreas
pToolBar->setFloatable(false);
pToolBar->setGeometry(300,0,160,30);
QToolButton *playButton=new QToolButton(pToolBar);
playButton->setIcon(QIcon("/images/play.png"));
playButton->setGeometry(10,0,40,30);
playButton->setToolTip("Play/Pause");
connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked()));
The Tool buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction().
Example:
QAction *newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcut(tr("Ctrl+N"));
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
You can use triggered signal, This signal is emitted when the given action is triggered.
Your example:
QToolButton *playButton=new QToolButton(pToolBar);
connect(playButton, SIGNAL(triggered()),SLOT(playButtonClicked()));
Try explicitly adding the toolbutton to the toolbar. The following code works perfectly for me:
QToolBar *pToolBar = new QToolBar(this);
QToolButton *playButton=new QToolButton(pToolBar);
playButton->setIcon(QIcon("/images/play.png"));
playButton->setText("Play");
playButton->setToolTip("Play/Pause");
playButton->setGeometry(10,0,40,30);
QAction *a = pToolBar->addWidget(playButton);
a->setVisible(true);
connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked()));
You should probably save the QAction pointer somewhere, since it's the easiest way to assign keyboard shortcuts, enable / disable the button etc. Let me know if this works for you. If it doesn't, perhaps posting a complete compilable example here will help us help you. You should be able to get a small demo program that shows your problem within one or two files.
Cheers,
As jordenysp indirectly explains, the API is QAction centric