, 나는 위젯의 몇 선언과 같이 :클래스 외부에서 위젯에 액세스 하시겠습니까? Qt는 창조주에
헤더 파일 :
class MapViewer : public QGraphicsView
{
Q_OBJECT
public:
explicit MapViewer(QGraphicsScene *scene, QWidget *parent = 0);
~MapViewer();
public slots:
void mousePressEvent(QMouseEvent *event);
};
// Declaration for the map editor window.
class MapEditor : public QMainWindow
{
Q_OBJECT
public:
explicit MapEditor(QWidget *parent = 0);
~MapEditor();
public:
QLayout *editorLayout;
QPushButton *btn;
QGraphicsScene *mapScene;
MapViewer *mapView;
private:
Ui::MapEditor *ui;
};
CPP 파일 :
MapEditor::MapEditor(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MapEditor)
{
ui->setupUi(this);
this->setWindowTitle("2DXY :: Map Editor");
this->setGeometry(10,10,1170,750);
editorLayout = new QVBoxLayout; // Create a new layout
this->setLayout(editorLayout); // Set the widget's layout to our newly created layout.
mapScene = new QGraphicsScene(); // Create a new graphics scene to draw upon.
mapView = new MapViewer(mapScene,this); // Create a new graphics view to display our scene - set its parent to 'this' so that it doesn't open in a new window.
mapView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mapView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mapView->setGeometry(20,20,1178,546); // Width first, then height.
AND :
void MapViewer::mousePressEvent(QMouseEvent *event)
{
// Show an empty message box, just to check that the event handler works!
QMessageBox *notification = new QMessageBox();
notification->show();
notification->exec();
// Some how access the same QGraphicsScene and View (mapScene, mapView) as above, so
// I can update their contents on the open form/window.
}
그리고 당신이 볼 수 있듯이, 나는 Graphics Scene에 다시 액세스하여 그것을 업데이트하고, 다시 그리기를 원한다. t (또는 무엇이든). 그러나 위젯을 여러 가지 방법으로 선언하는 데 몇 시간의 시행 착오를 겪었지만 그래픽 장면에 전혀 액세스 할 수 없습니다.
새 메시지 상자를 열거 나 디버그 창으로 출력하도록 설정되어 있다면 작동하며 이미 정의한 위젯에 액세스 할 수 없기 때문에 수신기 자체가 작동한다는 것을 알고 있습니다.
이 문제에 대한 (상대적으로) 쉬운 해결책이 있으며, 나는 분명히 뭔가 빠져있는 것 같습니다.
클래스의 멤버 변수를 만들고 getter 메소드를 제공하십시오 ... – cmannett85
@ cmannett85 - 이것을 자세히 설명하거나 예제를 제공해 주시겠습니까? (저는 C++을 처음 접했습니다!) 감사합니다! – BnMcG
브렛 쿤 (Bret Kuhns)은 그 일을 훌륭하게 해냈습니다. 그러나 이것이 C++을 배우는 방법이 아니라는 것을 강조해야합니다! Qt는 '일반적인'C++과 비교하여 일부 특이한/사용 불가능한 패러다임을 사용하므로 먼저 언어를 배우고 프레임 워크를 익히십시오. – cmannett85