How to acces button from another form



  • Hello

    I've got such a problem: I created a form with class and this form include secondone only .ui form (without class files, just a tamplate of layout). Inside this other form i've got a few buttons and I want to listen if their are pressed. It is not possible with option [go to slot]. I have to add the second form is inside a tabWidget, but buttons should do same action for any tab.

    Please advice me some solution.



  • If i understand correctly you try to apply your "second" form to one of the QTabWidget TabWidgets (:D)?
    I guess you can ui->setupUi(this) for every QTableWidget QWidget. This means that you can call wanted QWidget::Ui object - in this case QButton like tabWithCustomForm->ui->buttonOne if i understand correctly.



  • Acctualy I've solve this problem. What i needed were to get handler to QButton from the form added dynamicly to QTabWidget.
    I created 2 lists; forms and tabs(QWidget type) and for each form I used setupUi() for tabs.
    Due to QButton name was declared in diffrent form it was "invisible" from class attatched to main window form.
    As solution every time when tab is changed I looked for its handler by name
    ui->tabWidget->findChild<QPushButton *>("buttonName");
    then connect new handler to slot of button_pressed_function() after pressed() event
    Do You think it could by done without find metod?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you have any constraint to have your second widget as a UI only object ? Why not have it as normal widget and give it an interface that you can connect properly without the need to know the internals of the widget ?



  • I find QT Designer Form and at first moment I've thought that will do the work, but later I added buttons and things get more complicated. Actually I've added to QTabWidget new widgets and apply the form to them.
    I wonder if tabs added as form class( created by designer) have some implemented connection with containing the QTabWidget class(QMainWindow in my case), or it's necessary to pass a handle.


  • Lifetime Qt Champion

    There's no particular connections created automatically. It's your role to do that.

    You should create your widgets with designer, give them an interface so you can then easily re-use them within other widgets. By interface, I don't mean give access to their own internal widgets because that's an implementation detail that should not be known outside. e.g. if one of your widget is a QDialog based widget, you should not try to connect to the ok button that you have in that widget, but connect to the accepted signal from QDialog.

    On a side note, It's pretty unusual to put a QMainWindow in a QTabWidget.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.