16,216
社区成员
发帖
与我相关
我的任务
分享
//mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QDialog(parent), MsgHandler(parent)
{
QVBoxLayout *main_layout = new QVBoxLayout(this); //error: ambiguous conversion from derived class 'MainWindow' to base class 'QWidget':
setWindowTitle(tr("Test")); //error: non-static member 'setWindowTitle' found in multiple base-class subobjects of type 'QWidget':
}
// msg_handler.h
class MsgHandler : public QWidget
{
Q_OBJECT
public:
MsgHandler(QWidget *parent=nullptr);
~MsgHandler();
void popMsg1();
};
// msg_handler.cpp
MsgHandler::MsgHandler(QWidget *parent)
: QWidget(parent)
{
}
void MsgHandler::popMsg1()
{
QMessageBox::critical(this, tr("Error"), tr("There is an error"));
}
MsgHandler::~MsgHandler()
{
}
//mainwindow.h
class MainWindow: public QDialog, public MsgHandler
{
Q_OBJECT
public:
MainWindow(QWidget *parent=nullptr);
~MainWindow();
}
//mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QDialog(parent)
{
QVBoxLayout *main_layout = new QVBoxLayout(this); //error: ambiguous conversion from derived class 'MainWindow' to base class 'QWidget':
setWindowTitle(tr("Test")); //error: non-static member 'setWindowTitle' found in multiple base-class subobjects of type 'QWidget':
}
// msg_handler.cpp
MsgHandler::MsgHandler()
{
}
void MsgHandler::popMsg1()
{
QWidget w;
QMessageBox::critical(&w, tr("Error"), tr("There is an error"));
}
改成这样可以[/quote]
也不行,不继承就不是槽函数了,是普通函数,无法连接信号 。。。
// msg_handler.cpp
MsgHandler::MsgHandler()
{
}
void MsgHandler::popMsg1()
{
QWidget w;
QMessageBox::critical(&w, tr("Error"), tr("There is an error"));
}
改成这样可以
// msg_handler.cpp
MsgHandler::MsgHandler()
{
}
void MsgHandler::popMsg1()
{
if(QWidget *win = static_cast<QWidget *>(this)) {
QMessageBox::critical(win, tr("Error"), tr("There is an error"));
}
}
MsgHandler::~MsgHandler()
{
}