Question : Qt signals / slots not working

Hi,
I have a .ui file created with QtDesigner and am trying to connect a menu item to a class.

The first snippet of code shows my main function (ignore the ogre stuff, you can just assume that CustomOgreWidget is any arbitrary class).

The .ui file has the action called actionSelect_Object and the slot is defined correctly in CustomOgreWidget.

However, when I select Open Project when running the app, the code never jumps into the SlotOpenProject function.

If it's useful, QObject, SIGNAL, triggered and SLOT' are underlined with a quiggly red line in Visual Studio.

I have created other connections in other classes (i.e. the QTimer loop for rendering the Ogre view) and these work perfectly fine. I just can't work out why this isn't working.

Any help is greatly appreciated!

Cheers
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
int main(int argc, char *argv[])
{
  // Create application
  QApplication* mainApp = new QApplication(argc, argv);

  // Create an OgreWidget
  CustomOgreWidget* mOgreWidget = new CustomOgreWidget(0, 0);
  mOgreWidget->loadRenderSystemsFromPlugins();

  // Create a MainWindow
  QMainWindow* mainWin = new QMainWindow();

  // Setup user interface for MainWindow
  Ui::ApplicationClass ui;
  ui.setupUi(mainWin);
 
  // Show MainWindow and add OgreWidget
  mainWin->showMaximized();
  mainWin->setCentralWidget(mOgreWidget);

  // Initialise Ogre
  mOgreWidget->initialiseOgre();

  // Setup Ogre parameters
  Ogre::NameValuePairList ogreWindowParams;
  ogreWindowParams["FSAA"] = "";
  mOgreWidget->initialise(&ogreWindowParams);
 
  QObject::connect(ui.actionSelect_Object, SIGNAL(triggered()), mOgreWidget, SLOT(SlotOpenProject()) );

  // Run application loop
  return mainApp->exec();
  }

Answer : Qt signals / slots not working

I believe that you need to add a Q_OBJECT to classes which inherit from QObject:

class CustomOgreWidget :
  public OgreWidget
{
  Q_OBJECT

public:
  CustomOgreWidget(QWidget* parent=0, Qt::WindowFlags f=0);
  ~CustomOgreWidget(void);

public slots:
  void SlotOpenProject(void);
};
Random Solutions  
 
programming4us programming4us