Having form A access form B

  • In my Qt project I have two UIs (QMainWindow, QDialog) Form A and Form B. How can I make it so that when 'gobutton' on Form B is pressed Form A changes the current index of a tab menu to 1? I have looked at a few things, but they all seem outdated or just didn't work.

    TL;DR What's the easiest way to make a button press in one file trigger an event in another?

  • Qt Champions 2017

    fastest way is a signal & slot.

  • @mrjj I tried using slots or signals, but I got mixed up in connecting a signal from one file to a slot in another. The example in the documentation ("A Small Example") does not showcase or go over how it's done when attempting to do cross-file/cross-ui. Is there a better tutorial covering this?

  • Qt Champions 2017

    try this
    If still unclear let me know and ill try to explain.

    // How it works:
    // We make new signal for dialog to send
    class MyDialog : public QDialog {
      void ChangeTab(int index); <<< new signal. 
    // and for GO button we do
    void MyDialog::on_Go_clicked() {
      emit ChangeTab( 1 ); // tell the world
    // then in main we do
    void MainWindow::on_ShowDialog_pressed() {
      MyDialog dialog;
    // connect directly from dialogs new signal to the slot for index for the tabwidget
      connect(&dialog, SIGNAL(ChangeTab(int)), ui->tabWidget, SLOT(setCurrentIndex(int)));
      dialog.exec(); // show it

  • @mrjj Thanks :D That example worked perfectly. I assume it is the same if I want to append to a TextBox correct?

  • Qt Champions 2017


    i assume it is the same if I want to append to a TextBox correct?

    Well almost, but signal and slot have parameters so make sure to match it up.

    void QTextEdit::append(const QString &text)

    to send a signal to that slot, it must be a string we sent.
    Say our signal from before
    void ChangeTab(int index);
    is no good as that is an int.
    however if we made new signal
    void ChangeStatusText(const QString &text);
    that would match the widgets slot.

    But yes very the same.

  • @mrjj Sorry to re-open this thread, but I've one last question whcih my search terms haven't seemed to answer. Is there a way I can emit a signal from something that is not a UI?
    I have a file called functions.cpp/.h which dialog.cpp calls out to after the go button is pressed, but I want to ->append() things to a textBrowser.

  • Hi, yes that is possible. Make sure your class inherit QObject and contains the Q_OBJECT macro. That way your class will get registered to Qt's metasystem which allows you to send signals.

Log in to reply

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