当我尝试在Qt中连接到QLabel时,我的代码用红线加下划线。错误消息的文本是:
expected token ';' got ')'
我尝试在connect expected token ';' got ')'的帮助下修复该问题,但它没有帮助。 mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->desert, SIGNAL(toggled(true)), ui->label, SLOT(setPixmap(QPixmap("D:/Qt Projects/DZ3/Desert.jpeg"))));
QObject::connect(ui->koala, SIGNAL(toggled(true)), ui->label, SLOT(setPixmap(QPixmap("D:/Qt Projects/DZ3/Koala.jpeg"))));
QObject::connect(ui->penguins, SIGNAL(toggled(true)), ui->label, SLOT(setPixmap(QPixmap("D:/Qt Projects/DZ3/Penguins.jpeg"))));
}
MainWindow::~MainWindow()
{
delete ui;
}
沙漠,考拉和企鹅都是QRadioButtons,标签是QLabel。我借助Design Tab将它们粘贴在mainwindow.ui的项目中。并且在帖子开头有一个问题。
带下划线的字符串:
QObject::connect(ui->desert, SIGNAL(toggled(true)), ui->label, SLOT(setPixmap(QPixmap("D:/Qt Projects/DZ3/Desert.jpeg"))));
QObject::connect(ui->koala, SIGNAL(toggled(true)), ui->label, SLOT(setPixmap(QPixmap("D:/Qt Projects/DZ3/Koala.jpeg"))));
QObject::connect(ui->penguins, SIGNAL(toggled(true)), ui->label, SLOT(setPixmap(QPixmap("D:/Qt Projects/DZ3/Penguins.jpeg"))));
实际上,代码编译,但在切换QRadioButtons后没有任何反应,所以在此之后标签上没有任何变化。
字符串ui->label->setPixmap(QPixmap("D:/Qt Projects/DZ3/Desert.jpeg"))
有效。
我应该在代码中纠正什么?
答案 0 :(得分:1)
qt_static_metacall
生成类似if (_c == QMetaObject::InvokeMetaMethod) {
QLabel *_t = static_cast<QLabel *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->setPixmap((*reinterpret_cast< int(*)>(_a[1]))); break;
的方法。您的代码与来电一样:
setPixmap
因此声明中的变量不适用于回调&#39; setPixmap&#39;在运行时执行。您需要自己声明方法并使用必要的参数调用QObject::connect(ui->desert, SIGNAL(toggled(bool)), this, SLOT(mySlot(bool)));
// Something code
void MainWindow::mySlot(bool)
{
QString value;
// something for get correct value
ui->label->setPixmap(value);
}
。例如:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Main extends Application{
public void start(Stage primaryStage) throws Exception{
Font.loadFont(Main.class.getResource("TRON.TTF").toExternalForm(),10);
StackPane pane = new StackPane();
Label label = new Label("Text");
pane.getChildren().add(label);
label.setStyle("-fx-font-family:'TRON'; -fx-font-size: 50px;-fx-text-fill: red");
Scene scene = new Scene(pane,300,300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
抱歉我的英文!
答案 1 :(得分:0)
我在mainwindow.cpp中使用此代码完成了它:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_desert_toggled(bool checked)
{
ui->label->setPixmap(QPixmap("D:/Qt Projects/DZ3/Desert.jpeg"));
}
void MainWindow::on_koala_toggled(bool checked)
{
ui->label->setPixmap(QPixmap("D:/Qt Projects/DZ3/Koala.jpeg"));
}
void MainWindow::on_penguins_toggled(bool checked)
{
ui->label->setPixmap(QPixmap("D:/Qt Projects/DZ3/Penguins.jpeg"));
}
无论如何,感谢所有试图帮助我的人!