QT没有匹配的初始化构造函数

QT no matching constructor for initialization

本文关键字:初始化 构造函数 QT      更新时间:2023-10-16

我在尝试移植到QT5的通知系统中得到以下错误:

mainwindow.cpp:9: error: no matching constructor for initialization of 'TrayNotificationManager' 
tnm = new TrayNotificationManager(this);

你知道有什么问题吗?

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
#include "traynotificationmanager.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void closeEvent(QCloseEvent *);
private slots:
    void on_actionShow_Notification_Widget_triggered();
private:
    Ui::MainWindow *ui;
    TrayNotificationManager *tnm;
};
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tnm = new TrayNotificationManager(this);
}
MainWindow::~MainWindow()
{
    delete ui;
    delete tnm;
}
void MainWindow::closeEvent(QCloseEvent *)
{
    QApplication::quit();
}
void MainWindow::on_actionShow_Notification_Widget_triggered()
{
    QIcon* icon = new QIcon(":/icons/info.png");
    TrayNotificationWidget* trayNotification = new TrayNotificationWidget(icon->pixmap(64, 64), "Test", "This is a test message.");
    tnm->append(trayNotification);
}

traynotificationmanager.h:

#ifndef TRAYNOTIFICATIONMANAGER_H
#define TRAYNOTIFICATIONMANAGER_H
#include <QtCore>
#include "traynotificationwidget.h"
class TrayNotificationManager : public QObject
{
    Q_OBJECT
signals:
public slots:
    void removeFirst(TrayNotificationWidget *widget);
public:
    TrayNotificationManager();
    ~TrayNotificationManager();
    void append(TrayNotificationWidget *widget);
    void clear();
    void setMaxTrayNotificationWidgets(int max);
private:
    QList<TrayNotificationWidget*>* notificationWidgets;
    int m_deltaX;
    int m_deltaY;
    int m_startX;
    int m_startY;
    int m_width;
    int m_height;
    bool m_up;
    int m_onScreenCount;
    int m_maxTrayNotificationWidgets;
};
#endif // TRAYNOTIFICATIONMANAGER_H

traynotificationmanager.cpp:

#include "traynotificationmanager.h"
TrayNotificationManager::TrayNotificationManager()
{
    notificationWidgets = new QList<TrayNotificationWidget*>();
    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect clientRect = desktopWidget->availableGeometry();
    m_maxTrayNotificationWidgets = 4;
    m_width = 280;
    m_height = 100;
    m_onScreenCount = 0;
#ifdef Q_WS_MACX
    m_startX = clientRect.width() - m_width;
    m_startY = 10;
    m_up = false;
#endif
#ifdef Q_WS_X11
    m_startX = clientRect.width() - m_width;
    m_startY = 10;
    m_up = false;
#endif
#ifdef Q_WS_WIN
    m_startX = clientRect.width() - m_width;
    m_startY = clientRect.height() - m_height;
    m_up = true;
#endif
    m_deltaX = 0;
    m_deltaY = 0;
}
TrayNotificationManager::~TrayNotificationManager()
{
    notificationWidgets->clear();
    delete notificationWidgets;
}
void TrayNotificationManager::setMaxTrayNotificationWidgets(int max)
{
    this->m_maxTrayNotificationWidgets = max;
}
void TrayNotificationManager::append(TrayNotificationWidget* widget)
{
    connect(widget, SIGNAL(deleted(TrayNotificationWidget*)), this, SLOT(removeFirst(TrayNotificationWidget*)));
    qDebug() << "Count: " + QVariant(notificationWidgets->count()).toString();
    if(notificationWidgets->count() < m_maxTrayNotificationWidgets)
    {
        qDebug() << "Before: " + QVariant(m_deltaY).toString();
        if(notificationWidgets->count() > 0)
        {
            if(m_up)
                m_deltaY += -100;
            else
                m_deltaY += 100;
        }
        if(notificationWidgets->count() == 0)
            m_deltaY = 0;
        qDebug() << "After: " + QVariant(m_deltaY).toString();
    }
    else
    {
        m_deltaY = 0;
    }
    widget->setGeometry(m_startX + m_deltaX, m_startY + m_deltaY, m_width, m_height);
    notificationWidgets->append(widget);
}
void TrayNotificationManager::removeFirst(TrayNotificationWidget *widget)
{     
    int i = notificationWidgets->indexOf(widget);
    qDebug() << "Count: " + QVariant(notificationWidgets->count()).toString();
    qDebug() << "Index: " + QVariant(i).toString();
    if(notificationWidgets->count() > 0)
    {
        notificationWidgets->takeAt(i)->deleteLater();
        qDebug() << "Removing TrayNotificationWidget";
    }
}
void TrayNotificationManager::clear()
{
    for(int i = 0; i < notificationWidgets->count(); i++)
    {
        delete notificationWidgets->takeAt(i);
        qDebug() << "Removing TrayNotificationWidget";
    }
}

traynotificationwidget.h:

#ifndef TRAYNOTIFICATIONWIDGET_H
#define TRAYNOTIFICATIONWIDGET_H
#include <QWidget>
#include <QtGui>
class TrayNotificationWidget : public QWidget
{
    Q_OBJECT
public:
    explicit TrayNotificationWidget(QPixmap pixmapIcon, QString headerText, QString messageText);
private:
    QTimer* timeout;
signals:
    void deleted(TrayNotificationWidget*);
public slots:
   void fadeOut();
};
#endif // TRAYNOTIFICATIONWIDGET_H

traynotificationwidget.cpp:

#include "traynotificationwidget.h"
TrayNotificationWidget::TrayNotificationWidget(QPixmap pixmapIcon, QString headerText, QString messageText) : QWidget(0)
{
    setWindowFlags(
        #ifdef Q_OS_MAC
            Qt::SubWindow | // This type flag is the second point
        #else
            Qt::Tool |
        #endif
            Qt::FramelessWindowHint |
            Qt::WindowSystemMenuHint |
            Qt::WindowStaysOnTopHint
        );
    setAttribute(Qt::WA_NoSystemBackground, true);
    // set the parent widget's background to translucent
    setAttribute(Qt::WA_TranslucentBackground, true);
    //setAttribute(Qt::WA_ShowWithoutActivating, true);
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    // create a display widget for displaying child widgets
    QWidget* displayWidget = new QWidget;
    displayWidget->setGeometry(0, 0, this->width(), this->height());
    displayWidget->setStyleSheet(".QWidget { background-color: rgba(0, 0, 0, 75%); border-width: 1px; border-style: solid; border-radius: 10px; border-color: #555555; } .QWidget:hover { background-color: rgba(68, 68, 68, 75%); border-width: 2px; border-style: solid; border-radius: 10px; border-color: #ffffff; }");
    QLabel* icon = new QLabel;
    icon->setPixmap(pixmapIcon);
    icon->setMaximumSize(32, 32);
    QLabel* header = new QLabel;
    header->setMaximumSize(225, 50);
    header->setWordWrap(true);
    header->setText(headerText);
    header->setStyleSheet("QLabel { color: #ffffff; font-weight: bold; font-size: 12px; }");
    QLabel* message = new QLabel;
    message->setMaximumSize(225, 100);
    message->setWordWrap(true);
    message->setText(messageText);
    message->setStyleSheet("QLabel { color: #ffffff; font-size: 10px; }");
    QHBoxLayout* displayMainLayout = new QHBoxLayout;
    displayMainLayout->addWidget(icon);
    QVBoxLayout* vl = new QVBoxLayout;
    vl->addWidget(header);
    vl->addWidget(message);
    displayMainLayout->addLayout(vl);
    displayWidget->setLayout(displayMainLayout);
    QHBoxLayout* containerLayout = new QHBoxLayout;
    containerLayout->addWidget(displayWidget);
    setLayout(containerLayout);
    show();
    timeout = new QTimer(this);
    connect(timeout, SIGNAL(timeout()), this, SLOT(fadeOut()));
    timeout->start(3000);
}
void TrayNotificationWidget::fadeOut()
{
    emit deleted(this);
    this->hide();
}

声明中没有构造函数的参数。

重装:

explicit TrayNotificationManager(QWidget* parent = 0);

或者不带"this"参数调用