Qt Mouse Event Receiving issue - c++

This might seem as too simple question, but I couldn't find the answer for too long, so I've decided to ask a question here.
I have a class derived from QFrame. It contains two buttons.
Issue: When I set a parent to those buttons as "this" they do show up but they have no reaction to the mouse. When I set as parent parent of my QFrame it works:
When:
m_btnCompile = new ApproxGUIMenuButton(this);
m_btnSettings = new ApproxGUIMenuButton(this);
Doesn't work
When:
m_btnCompile = new ApproxGUIMenuButton(parentWidget());
m_btnSettings = new ApproxGUIMenuButton(parentWidget());
Works
Second option isn't solution for me because I need buttons to be in local coordinate system.
Parent is generated by QDesigner. I'm working in Visual Studio 2013 if it's important. What do I need to do? Please, help.

Problem solved but I still don't know a reason. I've added new member derived from QWidget and inside it set it as parent to those buttons and it worked.

Related

How to get UI objects to only appear in dialog AFTER button is clicked

I have a dialog that initially has several buttons, let's call them Write, View, OK, and Cancel.
The way it should to is to have the dialog upon creation only have those three buttons and nothing more.
When the Write button is cancelled, it's supposed to create a QLineEdit object in the window above the buttons where the user can enter a new string,which when OK is then clicked will be added to an external QStringList.
When View is clicked, LineEdit should go away (if it's up) and a QListView to come up instead to view everything in that list.
The problem is, I know how to use hide() to get objects that are already in the dialog to NOT appear.
but I am having trouble figuring out how to get an object not currently on the table to appear. I'm new to using Qt so it may be something easy I'm just accidentally overlooking (in fact I hope it is).
Could anyone please offer advice? Thanks!
Just create the items normally and then set:
ui->control->setVisible(false);
after you have created the UI (after ui->setupUi(this);) possibly in the constructor (in case you use code generated by Qt Creator).
And when you need them:
ui->control->setVisible(true);
Doc for this:
http://qt-project.org/doc/qt-4.8/qwidget.html#visible-prop
when using a QListView you should also have a QListModel that provides the data to it, if you only have QStrings then a QStringListModel is premade for you to use
to add a row you can do:
int rows = model->rowCount();
model->addRow(rows,1);
QModelIndex index = model->index(rows,0);
model->setData(index, string);

How do you adjust the window decoration size on a QMainWindow?

I was wondering how you adjust the size (particularly height) of the window decoration on a QMainWindow. I want behaviour similar to the QtDesigner, Xcode and Skype. I tried adding a QToolBar with the following code:
QMainWindow *mainWindow = new QMainWindow();
QToolBar *bar = new QToolBar();
bar->setFixedHeight(100);
bar->setFloatable(false);
bar->setMovable(false);
mainWindow->addToolBar(bar);
mainWindow->show();
Compared to what I want it is still a long way off - :
On the left you can see what I want and on the right you can see what I currently have.
Is this possible in Qt? If yes, how would this effect be accomplished?
The solution was simple. All I had to do is call:
mainWindow->setUnifiedTitleAndToolBarOnMac(true);
Problem solved!
Edit, 2013-08-19:
It appears that as of Qt5, you need to pull this git repository and build the library - then link it to your project. In order to set the unified title and toolbar, see the provided examples in the repository.

Creating a DialogBar with MFC with my own controls

I'm turning mad with a little problem with Visual Studio 2008 and MFC. I'm working on a project actually and I'm trying to create an SDI Application. Right, now I want to add a dockable DialogBar. Well, the first time I added it with the resource view, I could create the bar without problems.
But... I want to add some controls like a couple of ListBox, buttons and so on. The program doesn't allows me to create member variables for these controls. The option appears in grey.
Searching a little, I found that it's mandatory to create a new class derived from CDialogBar and "enhance" it with the Message handler and so on. I've been reading some tutorials and it didn't work for me.
Can you please help me? I'm starting to get nervous, the use of dialogbars is mandatory in my design and I've never implemented them before.
Thank you very much, and sorry for my poor english.
You can add a control member variable by hand instead of using the wizard. All it takes is a call to DDX_Control in the dialog bar's DoDataExchange function.
But CDialogBar-derived classes do not handle control clicks. The CDialogBar class forwards all of those messages to the dialog bar's parent window. So be prepared to add the message handlers to the parent (usually CMainFrame).
For learning purposes you might try creating your dialog bar as a CDialog first, to see the proper code lines and message map entries supplied by the wizard. Then you can copy/move these details as appropriate into your actual code project.

Qt QMainWindow central widget deletion

my application requires the user to switch between several screens. The way I'm doing this is by creating different QFrames for each screen, and then setting the Qframes as central widgets on the MainWindow. The problem is that every time I call setCentralWidget(frame), the old frame gets deleted and I can't access it later. How can save that old frame so that I can access it later?
Please let me know if I am unclear in my question.
You can remove your central widget from QMainWidow reparenting it. Then, you could set new centralWidget;
QWidget* savedWidget = mainWnd->centralWidget();
savedWidget->setParent(0);//now it is saved
mainWnd->setCentralWidget(newWidget);
Also using QStackedWidget possibly would be better solution.
QStackedWidget is an elegant solution for this problem, you can find out how to use it properly here.
You can play around with .hide()/.show() on the appropriate subwidgets to accomplish this. But a better solution for your case is almost certainly to use a QTabWidget or QStackedWidget.

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.