PDA

View Full Version : error: request for member 'show' is ambiguous w.show();



rahulvishwakarma
24th June 2020, 11:42
hi to all, i've Qt 5.7 in Centos 7 in VM, i am trying to build small program.


#include "mymainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyMainWindow w;
w.show();

return a.exec();
}

in qwidget.h :-


public Q_SLOTS:
// Widget management functions

virtual void setVisible(bool visible);
void setHidden(bool hidden);
void show(); // <-- here is showing error
void hide();

void showMinimized();
void showMaximized();
void showFullScreen();
void showNormal();

bool close();
void raise();
void lower();

Ginsengelf
24th June 2020, 12:49
Hi, please show the declaration (.h file) for MyMainWindow.

Ginsengelf

rahulvishwakarma
24th June 2020, 13:06
mine mymainwindow.h :-



#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
//#include <QtWidgets>

#include <QMainWindow>
#include <QFile>
#include <QLabel>
#include <QStringList>
#include <QString>
#include <QTableWidget>

#include "cell.h"
#include "spsheetdialog.h"
#include "gotocelldialog.h"

class GoToCellDialog;
class FindDialog;
class SPsheetDialog;
class Cell;

namespace Ui {
class MyMainWindow;
}

class MyMainWindow : public QMainWindow, public QTableWidget
{
Q_OBJECT

public:
explicit MyMainWindow(QWidget *parent = 0);
~MyMainWindow();

private slots:
void on_actionNew_triggered();

void on_actionOpen_triggered();

void on_actionSave_triggered();

void on_actionSave_As_triggered();

void on_actionGoTo_cell_triggered();

void on_actionSort_triggered();

void on_actionAbout_triggered();

void openRecentFile();

void updateStatusBar();

void spreadsheetModified();

private:
Ui::MyMainWindow *ui;

QStringList recentFiles;
SPsheetDialog *spsheet;
Cell *cell;

void readSettings();
void writeSettings();
bool okToContinue();
void connectActions();
void configureStatusBar();

void setCurrentFile(const QString &fileName);
void updateRecentFileActions();
bool loadFile(const QString &fileName);
bool savefile(const QString &fileName);
QString strippedName(const QString &fullFileName);

QLabel *labelLocation;
QLabel *labelFormula;
enum { MaxRecentFiles = 5 };
QString curFile;
};

#endif // MYMAINWINDOW_H

Ginsengelf
24th June 2020, 13:59
Hi, I am not sure if it is possible to use multiple inheritance with two classes which each inherit QObject. You should probably split your widget in the part that's derived from QMainWindow, and a second widget that's derived from QTableWidget. Then use QMainWindow::setCentralWidget() to put the table widget into the main window.

Ginsengelf

d_stranz
24th June 2020, 17:39
I am not sure if it is possible to use multiple inheritance with two classes which each inherit QObject.

It might be allowed, but is ambiguous and bad design. QMainWindow and QTableWidget both inherit from QWidget (and QObject) so every call to a QWidget or QObject method is ambiguous - as the OP's error demonstrates. Does his code want to call the show() method for the QWidget that QMainWindow is derived from, or the show() method for the QWidget that QTableWidget is derived from? The compiler can't tell when there is a common base class for two different inheritance trees.

Even if the code qualified the call to show with one of the two base classes, it probably still wouldn't work correctly because the show() method for -both- base classes needs to be called in order to correctly layout the geometry of the two widgets. Bad design.

Not only that, but this model violates the C++ "Is-A" convention: MyMainWindow is -not- a QTableWidget, it is-a QMainWindow. It -uses- a QTableWidget, so the design should use the "composition" convention to create a main window that is "composed" of menus, tool bars, status bars, dock windows, and a table widget.