构造函数有问题吗?

发布于 2024-11-05 12:32:05 字数 1908 浏览 0 评论 0原文

你好 我在代码中遇到问题,以下代码重新调整

没有匹配函数来调用 QpushButton::QPushButton(QString*&,QWidget*&)
并且

没有调用 QPainter::drawText(const QPointF&, const QString&) 的匹配函数

,并且代码是

MainWindow::MainWindow(QWidget *parent) :
    QPushButton(parent)
{
    //ui->setupUi(this);
    //connect(this,SIGNAL(clicked()),this,SLOT(newWindow()));
}


MainWindow::MainWindow(QString *str,QWidget *parent) :
    QPushButton(str,parent)
{
    //ui->setupUi(this);
    text_str=str;
    connect(this,SIGNAL(clicked()),this,SLOT(newWindow()));
}

MainWindow::~MainWindow()
{
    //delete ui;
}

void MainWindow::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    QPen pen(Qt::black);
    pen.setWidth(6);
    painter.setPen(pen);

    static const QPointF points[6] = {
        QPointF(300.0, 160.0),//Top Line
        QPointF(220.0, 160.0),//
        //QPointF(300.0, 180.0),
        QPointF(195.0, 210.0),
        QPointF(220.0, 260.0),//
        QPointF(300.0, 260.0),//Bottom Line
        QPointF(325.0, 210.0)
    };

    painter.drawPolygon(points, 6);

    QPainter painter1(this);
    QPen pen1(Qt::green);
    painter1.setPen(pen1);
    QLinearGradient grad1(300, 160, 325, 260);
    QBrush brush1(grad1);
    grad1.setColorAt(1.0, Qt::white);
    painter1.setBrush(brush1);
    QFont font("Times", 12);
    painter1.setFont(font);
    QPoint point1 = QPoint( 240, 225);
    painter1.drawText( point1, text_str );
}

void MainWindow::newWindow()
{
    FrameWindow *frm=new FrameWindow(this);
    frm->show();
}

并且 mainnWindow 类由以下代码调用

NewWindow::NewWindow(QWidget *parent) : QMainWindow(父), ui(新用户界面::新窗口) { ui->setupUi(this); w1 = new MainWindow("你好",this); w1->show(); w6->show(); }

新窗口::~新窗口() { 删除用户界面; }

请帮我。 提前感谢您的帮助。

Hi
I have problem in code that the following code retuning the error that

no matching function for call to QpushButton::QPushButton(QString*&,QWidget*&)
and

no matching function for call to QPainter::drawText(const QPointF&, const QString&)

and the code is

MainWindow::MainWindow(QWidget *parent) :
    QPushButton(parent)
{
    //ui->setupUi(this);
    //connect(this,SIGNAL(clicked()),this,SLOT(newWindow()));
}


MainWindow::MainWindow(QString *str,QWidget *parent) :
    QPushButton(str,parent)
{
    //ui->setupUi(this);
    text_str=str;
    connect(this,SIGNAL(clicked()),this,SLOT(newWindow()));
}

MainWindow::~MainWindow()
{
    //delete ui;
}

void MainWindow::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    QPen pen(Qt::black);
    pen.setWidth(6);
    painter.setPen(pen);

    static const QPointF points[6] = {
        QPointF(300.0, 160.0),//Top Line
        QPointF(220.0, 160.0),//
        //QPointF(300.0, 180.0),
        QPointF(195.0, 210.0),
        QPointF(220.0, 260.0),//
        QPointF(300.0, 260.0),//Bottom Line
        QPointF(325.0, 210.0)
    };

    painter.drawPolygon(points, 6);

    QPainter painter1(this);
    QPen pen1(Qt::green);
    painter1.setPen(pen1);
    QLinearGradient grad1(300, 160, 325, 260);
    QBrush brush1(grad1);
    grad1.setColorAt(1.0, Qt::white);
    painter1.setBrush(brush1);
    QFont font("Times", 12);
    painter1.setFont(font);
    QPoint point1 = QPoint( 240, 225);
    painter1.drawText( point1, text_str );
}

void MainWindow::newWindow()
{
    FrameWindow *frm=new FrameWindow(this);
    frm->show();
}

and the mainnWindow class is called by the following code

NewWindow::NewWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::NewWindow)
{
ui->setupUi(this);
w1 = new MainWindow("Hello",this);
w1->show();
w6->show();
}

NewWindow::~NewWindow()
{
delete ui;
}

Please help me.
Advance Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流星番茄 2024-11-12 12:32:05
  1. QPushButton 的构造函数采用 QString 引用而不是指针,请参阅 http://doc.trolltech.com/4.7.1/qpushbutton.html#QPushButton-2 所以你应该改变你的构造函数MainWindow::MainWindow(QString *str,QWidget *parent)MainWindow::MainWindow(const QString &str,QWidget *parent) 或类似的。
  2. 我不太明白 drawText() 错误,因为签名看起来没问题。有点不清楚 text_str 是什么类型。如果这是一个指向 QString 的指针,它也不起作用,但错误消息应该略有不同。 (此外,您唯一的drawText调用中给出的点不是QPointF而是QPoint,所以我认为错误和您发布的代码之间存在不一致的地方)
  1. QPushButton's constructor takes a QString reference not a pointer, see http://doc.trolltech.com/4.7.1/qpushbutton.html#QPushButton-2 So you should change your constructor MainWindow::MainWindow(QString *str,QWidget *parent) to MainWindow::MainWindow(const QString &str,QWidget *parent) or similar.
  2. The drawText() error I don't quite get, because the signature seems ok. It is a bit unclear, what type text_str is. If that is a pointer to QString it would also not work, but the error message should be slightly different. (Also the point given in your only drawText call is not QPointF but QPoint, so I think there is something inconsistent between the errors and the code you posted)
夏见 2024-11-12 12:32:05

1) 正如已经指出的,QPushButton 没有采用 QString * & 的构造函数; QWidget * 作为参数。我认为您可能对通过引用传递和传递指针感到困惑。

2) DrawText 的函数签名是,

void QPainter::drawText ( const QPoint & position, const QString & text )

而 text_str 似乎是指向 QString 的指针。
所以使用,

painter1.drawText( point1, *text_str );

1) As already pointed out QPushButton has no constructor that takes QString * & QWidget * as arguments.I think you maybe be confused between passing by reference and passing a pointer.

2) The function signature for DrawText is

void QPainter::drawText ( const QPoint & position, const QString & text )

whereas text_str seems to be a pointer to QString.
So use,

painter1.drawText( point1, *text_str );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文