构造函数有问题吗?
你好 我在代码中遇到问题,以下代码重新调整
没有匹配函数来调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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)
或类似的。text_str
是什么类型。如果这是一个指向 QString 的指针,它也不起作用,但错误消息应该略有不同。 (此外,您唯一的drawText调用中给出的点不是QPointF而是QPoint,所以我认为错误和您发布的代码之间存在不一致的地方)QPushButton
's constructor takes aQString
reference not a pointer, see http://doc.trolltech.com/4.7.1/qpushbutton.html#QPushButton-2 So you should change your constructorMainWindow::MainWindow(QString *str,QWidget *parent)
toMainWindow::MainWindow(const QString &str,QWidget *parent)
or similar.text_str
is. If that is a pointer toQString
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)1) 正如已经指出的,QPushButton 没有采用 QString * & 的构造函数; QWidget * 作为参数。我认为您可能对通过引用传递和传递指针感到困惑。
2) DrawText 的函数签名是,
而 text_str 似乎是指向 QString 的指针。
所以使用,
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
whereas text_str seems to be a pointer to QString.
So use,