Important: Please read the Qt Code of Conduct -

[SOLVED] Adding onclick event to some Qwidget

  • Hi,

    I am relative new to Qt. I am going through some fast learning curve at the moment but there are some things I am stuck at.

    I have made a QT-cpp application with several widgets. It is actually meant to be a kind of wizard. It goes something like (pseudo code)

    @mainwindow->layout->addwidget(new headerWindow);
    QWidget *pWindow = new changeableWindow;
    mainwindow->layout->addwidget(new buttonControlWindow);@

    to pWindow I have added another widget like this

    @QUiLoader loader;
    QWidget *formWidget;

    QFile file(":/forms/textfinder.ui");;

    formWidget = loader.load(&file, this);

    I dont know exactly what is on formWidget, but when I click on a QSpinBox or QLineEdit I would like to perform some actions (execute a function). The normal signal/slot things dont seem to apply so I guess I have to work with events, but how?



  • to clarify you want to receive general click events even if the window widget contains other widgets like buttons etc and the user clicks on them?
    if you just want events for special widgets like a button or widgets that have a custom signal for it that would be easy i guess, click events might be consumed by the child widget and not passed to the main window but you can try that yourself when you implement the mouse event function in your window class:
    override the function QWidget::mouseReleaseEvent(QMouseEvent * event) and see if it gets called.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you give an example of what you would like to do with that function ? e.g. : write in QLineEdit -> call myFunc -> validate content

    Since you want to do some kind of wizard, did you took a look at QWizard ?

  • Hi,

    Thanks for the hint about QWizard. That really helps. Although I had built already some infrastructure I was wrestling with layout problems all day.

    Anyway the problem is as follows. I making another program that uses some ui-files as an input. Since ui-files are just xml-files that define controls, we also need to provide an xml file with valid values. Like this:


    comboBoxSpeed (like others) is control on the form. When I read this XML-file I set the valid values for this form. I have made this by hand, but it would be better if our customer could do it himself interactively. That's what the wizard will do. So if I select a QComboBox on the form it needs to highlight that QComboBox and a ComboBoxValidationWidget with valid values, default etc. will get called. The user sets the values and these are written to XML. Similar for QSpinBox and QLineEdit.

    If I re-implement QWidget::mouseReleaseEvent(QMouseEvent * event). How would I go about it? I need to process only widgets that have formWidget as parent. Or not?

  • Lifetime Qt Champion

    For the highlighting part you can use a QRubberBand

    As for editing, you could have a special mode for your application (e.g. like firebug for Firefox) where you have a transparent overlay widget that you would use to implement the mouse logic. The widget would grab the mouse, and each time you move it checks whether it can find an "editable" widget under, if so, show the QRubberBand on the widget. If the user clicks, start the correct editor.

    Hope it helps

  • I will investigate it. Thanks for comments. They have been very useful.

Log in to reply