1

I am trying to unit test (using Qt Test) the individual widgets in a Qt Widgets application, like menuBar, pushButton, etc. The project has been set up as a Subdirs project, where the both the Qt application and GUI test sub projects have their own directory and .pro file. The problem is that I cannot access any widget residing in MainWindow from the test class (MainWindowTest), as the compiler says:

  • error: cannot cast 'MainWindow' to its private base class 'Ui_MainWindow'
  • 'pushButton' is a private member of 'Ui_MainWindow'
  • constrained by private inheritance here

I tried to declare the MainWindowTest a friend in MainWindow, but it didn't help. I also tried to forward declare MainWindowTest in a separate header and then include it in mainwindow.h (a still declare MainWindowTest as friend), but that didn't help either.

Just creating a default subdir Widget application and a GuiTest AutoTest subdir project in Creator will produce all necessary code for this.

What I'm actually trying to achieve here, is to make a MainWindow instance in the private area of MainWindowTest and then write a couple of unit tests to check for the existence of the widgets and then do some keyboard and mouse event based testing. But, because I cannot even have access to the widgets, it would be terribly hard to do so.

I already looked through a couple of "solutions" over the net, but none of them seem to be clean solutions. I'd like to be able to execute QVERIFY2(win.pushButton, "pushButton wasn't created.") sort of thing right now, but as I have said, pushButton is inaccessible.

pusrob
  • 81
  • 7
  • https://stackoverflow.com/a/35846/6752050 – 273K Jun 11 '19 at 21:50
  • Thanks, but as I have written, I did use friend to do the trick. For some reason it didn't work. – pusrob Jun 11 '19 at 22:53
  • 1
    I can't guess without [mcve]. – 273K Jun 12 '19 at 00:11
  • OK, it would seem some preprocessor directives got better of me. By removing an #ifdef from around 'friend class MainWindowTest;' I can now access private members. Now only the ui_mainwindow.h generation remains to be a problem. – pusrob Jun 13 '19 at 13:25

0 Answers0