在另一个cpp文件中定义了svgview类的一个对象。
想在MainWindow中获取这个对象,该如何办?
thanks
14 个解决方案
#1
着急ing,求助...........
#2
要有相应的头文件吧?
#3
无代码无真相!(程序是调出来的^.^)
#4
有了头文件就可以了?
#5
有代码,我可以帮你测试。
无代码,我不懂得还是不懂。
无代码,我不懂得还是不懂。
#6
POSITION pos = GetFirstViewPosition();
while (pos != NULL)
{
CView* pView = GetNextView(pos);
// 判断pView是否能被转换成svgview类
if (NULL != dynamic_cast<svgview *>(pView))
{
// 哈哈,pView就是svgview
...
}
}
#7
Sorry, 上面的函数都是CDocument类的成员函数,因此必须首先获取CDocument类的指针。
#8
#include <QtGui>
#include "svgview.h"
#include "svgwindow.h"
SvgWindow::SvgWindow()
: QScrollArea()
{
QWidget *view = new QWidget(this);
renderer = SvgWindow::Native;//////////////////////////////////
setWidget(view);
}
void SvgWindow::openFile(const QString &file)
{
currentPath = file;
setRenderer(renderer);
}
QWidget* SvgWindow::getObject()
{
return view;
}
void SvgWindow::setRenderer(RendererType type)
{
renderer = type;
QWidget *view;
if (renderer == OpenGL) {
#ifndef QT_NO_OPENGL
view = new SvgGLView(currentPath, this);
#endif
} else if (renderer == Image)
view = new SvgRasterView(currentPath, this);
else
view = new SvgNativeView(currentPath, this);
getObject(view);
setWidget(view);
view->show();
}
void SvgWindow::mousePressEvent(QMouseEvent *event)
{
mousePressPos = event->pos();
scrollBarValuesOnMousePress.rx() = horizontalScrollBar()->value();
scrollBarValuesOnMousePress.ry() = verticalScrollBar()->value();
event->accept();
}
void SvgWindow::mouseMoveEvent(QMouseEvent *event)
{
if (mousePressPos.isNull()) {
event->ignore();
return;
}
horizontalScrollBar()->setValue(scrollBarValuesOnMousePress.x() - event->pos().x() + mousePressPos.x());
verticalScrollBar()->setValue(scrollBarValuesOnMousePress.y() - event->pos().y() + mousePressPos.y());
horizontalScrollBar()->update();
verticalScrollBar()->update();
event->accept();
}
void SvgWindow::mouseReleaseEvent(QMouseEvent *event)
{
mousePressPos = QPoint();
event->accept();
}
想获取view这个对象
#9
#include <QtGui>
#include "mainwindow.h"
#include "svgview.h"
#include "svgwindow.h"
#include <QLabel>
#include <QSplitter>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
int spinBox1value;
int spinBox2value;
MainWindow::MainWindow()
: QMainWindow()
{
QSplitter *MainSplitter = new QSplitter(Qt::Horizontal);
area = new SvgWindow;
SvgNativeView *getobject=area->getObject();
area->resize(289,maximumHeight ());
area->setStyleSheet("background-color:gray;");
QSplitter * WidgetRight = new QSplitter(Qt::Vertical);
//WidgetRight->setWindowTitle("Enter Your Age");
QWidget *SubWidget1 = new QWidget;
QWidget *SubWidget2 = new QWidget;
////////////////////////////////////////////////////////////////////////////////
spinBox1 = new QSpinBox;
QSlider *slider1 = new QSlider(Qt::Horizontal);
spinBox1->setRange(0, 130);
slider1->setRange(0, 130);
QObject::connect(spinBox1, SIGNAL(valueChanged(int)),
slider1, SLOT(setValue(int)));
QObject::connect(slider1, SIGNAL(valueChanged(int)),
spinBox1, SLOT(setValue(int)));
QObject::connect(spinBox1, SIGNAL(valueChanged(int)),
this,SLOT(GetValue1(int)));
QObject::connect(spinBox1, SIGNAL(valueChanged(int)),
getobject,SLOT(SpinBoxEvent(int)));
spinBox1->setValue(35);
//spinBox1value=spinBox1->value();
QHBoxLayout *layout1 = new QHBoxLayout;
layout1->addWidget(spinBox1);
layout1->addWidget(slider1);
SubWidget1->setLayout(layout1);
////////////////////////////////////////////////////////////////////////////
QSpinBox *spinBox2 = new QSpinBox;
QSlider *slider2 = new QSlider(Qt::Horizontal);
spinBox2->setRange(0, 130);
slider2->setRange(0, 130);
QObject::connect(spinBox2, SIGNAL(valueChanged(int)),
slider2, SLOT(setValue(int)));
QObject::connect(slider2, SIGNAL(valueChanged(int)),
spinBox2, SLOT(setValue(int)));
spinBox2->setValue(35);
QHBoxLayout *layout2 = new QHBoxLayout;
layout2->addWidget(spinBox2);
layout2->addWidget(slider2);
SubWidget2->setLayout(layout2);
/////////////////////////////////////////////////////////////////////////////
WidgetRight->addWidget(SubWidget1);
WidgetRight->addWidget(SubWidget2);
MainSplitter->addWidget(area);
MainSplitter->addWidget(WidgetRight);
setCentralWidget(MainSplitter);
QMenu *fileMenu = new QMenu(tr("&File"), this);
QAction *openAction = fileMenu->addAction(tr("&Open..."));
openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
QAction *quitAction = fileMenu->addAction(tr("E&xit"));
quitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
menuBar()->addMenu(fileMenu);
QMenu *rendererMenu = new QMenu(tr("&Renderer"), this);
nativeAction = rendererMenu->addAction(tr("&Native"));
nativeAction->setCheckable(true);
nativeAction->setChecked(true);
#ifndef QT_NO_OPENGL
glAction = rendererMenu->addAction(tr("&OpenGL"));
glAction->setCheckable(true);
#endif
imageAction = rendererMenu->addAction(tr("&Image"));
imageAction->setCheckable(true);
QActionGroup *rendererGroup = new QActionGroup(this);
rendererGroup->addAction(nativeAction);
#ifndef QT_NO_OPENGL
rendererGroup->addAction(glAction);
#endif
rendererGroup->addAction(imageAction);
menuBar()->addMenu(rendererMenu);
connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(rendererGroup, SIGNAL(triggered(QAction *)),
this, SLOT(setRenderer(QAction *)));
//setCentralWidget(area);
setWindowTitle(tr("SVG Viewer"));
}
void MainWindow::openFile(const QString &path)
{
QString fileName;
if (path.isNull())
fileName = QFileDialog::getOpenFileName(this, tr("Open SVG File"),
currentPath, "*.svg");
else
fileName = path;
if (!fileName.isEmpty()) {
area->openFile(fileName);
if (!fileName.startsWith(":/")) {
currentPath = fileName;
setWindowTitle(tr("%1 - SVGViewer").arg(currentPath));
}
}
}
void MainWindow::setRenderer(QAction *action)
{
if (action == nativeAction)
area->setRenderer(SvgWindow::Native);
#ifndef QT_NO_OPENGL
else if (action == glAction)
area->setRenderer(SvgWindow::OpenGL);
#endif
else if (action == imageAction)
area->setRenderer(SvgWindow::Image);
}
void MainWindow::GetValue1(int value1)
{
spinBox1value=spinBox1->value();
}
想在这个文件中获取上个文件的view对象
#10
public 属性,应该可以了,
你现在实现的问题应该也不大
你现在实现的问题应该也不大
#11
不行啊?我把 QWidget *view;定义成SvgWindow的public成员,然后再在MainWindow中写
extern QWidget *view;
出现错误:
error LNK2001: unresolved external symbol "class QWidget * view" (?view@@3PAVQWidget@@A)
extern QWidget *view;
出现错误:
error LNK2001: unresolved external symbol "class QWidget * view" (?view@@3PAVQWidget@@A)
#12
在一个类得头文件中把你要在类外使用的函数设定为Public,然后再MainWindow中添加#include “SvgWindow.h” 使用时SvgWindow::QWidget *view即可!试试看,应该没问题了!
#13
在这个cpp同名的.h文件中,添加上另一个cpp同名的.h引入后,声明一下你要调用的对象,应该就可以调用了。
#14
好的,我试试,谢谢
#1
着急ing,求助...........
#2
要有相应的头文件吧?
#3
无代码无真相!(程序是调出来的^.^)
#4
有了头文件就可以了?
#5
有代码,我可以帮你测试。
无代码,我不懂得还是不懂。
无代码,我不懂得还是不懂。
#6
POSITION pos = GetFirstViewPosition();
while (pos != NULL)
{
CView* pView = GetNextView(pos);
// 判断pView是否能被转换成svgview类
if (NULL != dynamic_cast<svgview *>(pView))
{
// 哈哈,pView就是svgview
...
}
}
#7
Sorry, 上面的函数都是CDocument类的成员函数,因此必须首先获取CDocument类的指针。
#8
#include <QtGui>
#include "svgview.h"
#include "svgwindow.h"
SvgWindow::SvgWindow()
: QScrollArea()
{
QWidget *view = new QWidget(this);
renderer = SvgWindow::Native;//////////////////////////////////
setWidget(view);
}
void SvgWindow::openFile(const QString &file)
{
currentPath = file;
setRenderer(renderer);
}
QWidget* SvgWindow::getObject()
{
return view;
}
void SvgWindow::setRenderer(RendererType type)
{
renderer = type;
QWidget *view;
if (renderer == OpenGL) {
#ifndef QT_NO_OPENGL
view = new SvgGLView(currentPath, this);
#endif
} else if (renderer == Image)
view = new SvgRasterView(currentPath, this);
else
view = new SvgNativeView(currentPath, this);
getObject(view);
setWidget(view);
view->show();
}
void SvgWindow::mousePressEvent(QMouseEvent *event)
{
mousePressPos = event->pos();
scrollBarValuesOnMousePress.rx() = horizontalScrollBar()->value();
scrollBarValuesOnMousePress.ry() = verticalScrollBar()->value();
event->accept();
}
void SvgWindow::mouseMoveEvent(QMouseEvent *event)
{
if (mousePressPos.isNull()) {
event->ignore();
return;
}
horizontalScrollBar()->setValue(scrollBarValuesOnMousePress.x() - event->pos().x() + mousePressPos.x());
verticalScrollBar()->setValue(scrollBarValuesOnMousePress.y() - event->pos().y() + mousePressPos.y());
horizontalScrollBar()->update();
verticalScrollBar()->update();
event->accept();
}
void SvgWindow::mouseReleaseEvent(QMouseEvent *event)
{
mousePressPos = QPoint();
event->accept();
}
想获取view这个对象
#9
#include <QtGui>
#include "mainwindow.h"
#include "svgview.h"
#include "svgwindow.h"
#include <QLabel>
#include <QSplitter>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
int spinBox1value;
int spinBox2value;
MainWindow::MainWindow()
: QMainWindow()
{
QSplitter *MainSplitter = new QSplitter(Qt::Horizontal);
area = new SvgWindow;
SvgNativeView *getobject=area->getObject();
area->resize(289,maximumHeight ());
area->setStyleSheet("background-color:gray;");
QSplitter * WidgetRight = new QSplitter(Qt::Vertical);
//WidgetRight->setWindowTitle("Enter Your Age");
QWidget *SubWidget1 = new QWidget;
QWidget *SubWidget2 = new QWidget;
////////////////////////////////////////////////////////////////////////////////
spinBox1 = new QSpinBox;
QSlider *slider1 = new QSlider(Qt::Horizontal);
spinBox1->setRange(0, 130);
slider1->setRange(0, 130);
QObject::connect(spinBox1, SIGNAL(valueChanged(int)),
slider1, SLOT(setValue(int)));
QObject::connect(slider1, SIGNAL(valueChanged(int)),
spinBox1, SLOT(setValue(int)));
QObject::connect(spinBox1, SIGNAL(valueChanged(int)),
this,SLOT(GetValue1(int)));
QObject::connect(spinBox1, SIGNAL(valueChanged(int)),
getobject,SLOT(SpinBoxEvent(int)));
spinBox1->setValue(35);
//spinBox1value=spinBox1->value();
QHBoxLayout *layout1 = new QHBoxLayout;
layout1->addWidget(spinBox1);
layout1->addWidget(slider1);
SubWidget1->setLayout(layout1);
////////////////////////////////////////////////////////////////////////////
QSpinBox *spinBox2 = new QSpinBox;
QSlider *slider2 = new QSlider(Qt::Horizontal);
spinBox2->setRange(0, 130);
slider2->setRange(0, 130);
QObject::connect(spinBox2, SIGNAL(valueChanged(int)),
slider2, SLOT(setValue(int)));
QObject::connect(slider2, SIGNAL(valueChanged(int)),
spinBox2, SLOT(setValue(int)));
spinBox2->setValue(35);
QHBoxLayout *layout2 = new QHBoxLayout;
layout2->addWidget(spinBox2);
layout2->addWidget(slider2);
SubWidget2->setLayout(layout2);
/////////////////////////////////////////////////////////////////////////////
WidgetRight->addWidget(SubWidget1);
WidgetRight->addWidget(SubWidget2);
MainSplitter->addWidget(area);
MainSplitter->addWidget(WidgetRight);
setCentralWidget(MainSplitter);
QMenu *fileMenu = new QMenu(tr("&File"), this);
QAction *openAction = fileMenu->addAction(tr("&Open..."));
openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
QAction *quitAction = fileMenu->addAction(tr("E&xit"));
quitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
menuBar()->addMenu(fileMenu);
QMenu *rendererMenu = new QMenu(tr("&Renderer"), this);
nativeAction = rendererMenu->addAction(tr("&Native"));
nativeAction->setCheckable(true);
nativeAction->setChecked(true);
#ifndef QT_NO_OPENGL
glAction = rendererMenu->addAction(tr("&OpenGL"));
glAction->setCheckable(true);
#endif
imageAction = rendererMenu->addAction(tr("&Image"));
imageAction->setCheckable(true);
QActionGroup *rendererGroup = new QActionGroup(this);
rendererGroup->addAction(nativeAction);
#ifndef QT_NO_OPENGL
rendererGroup->addAction(glAction);
#endif
rendererGroup->addAction(imageAction);
menuBar()->addMenu(rendererMenu);
connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(rendererGroup, SIGNAL(triggered(QAction *)),
this, SLOT(setRenderer(QAction *)));
//setCentralWidget(area);
setWindowTitle(tr("SVG Viewer"));
}
void MainWindow::openFile(const QString &path)
{
QString fileName;
if (path.isNull())
fileName = QFileDialog::getOpenFileName(this, tr("Open SVG File"),
currentPath, "*.svg");
else
fileName = path;
if (!fileName.isEmpty()) {
area->openFile(fileName);
if (!fileName.startsWith(":/")) {
currentPath = fileName;
setWindowTitle(tr("%1 - SVGViewer").arg(currentPath));
}
}
}
void MainWindow::setRenderer(QAction *action)
{
if (action == nativeAction)
area->setRenderer(SvgWindow::Native);
#ifndef QT_NO_OPENGL
else if (action == glAction)
area->setRenderer(SvgWindow::OpenGL);
#endif
else if (action == imageAction)
area->setRenderer(SvgWindow::Image);
}
void MainWindow::GetValue1(int value1)
{
spinBox1value=spinBox1->value();
}
想在这个文件中获取上个文件的view对象
#10
public 属性,应该可以了,
你现在实现的问题应该也不大
你现在实现的问题应该也不大
#11
不行啊?我把 QWidget *view;定义成SvgWindow的public成员,然后再在MainWindow中写
extern QWidget *view;
出现错误:
error LNK2001: unresolved external symbol "class QWidget * view" (?view@@3PAVQWidget@@A)
extern QWidget *view;
出现错误:
error LNK2001: unresolved external symbol "class QWidget * view" (?view@@3PAVQWidget@@A)
#12
在一个类得头文件中把你要在类外使用的函数设定为Public,然后再MainWindow中添加#include “SvgWindow.h” 使用时SvgWindow::QWidget *view即可!试试看,应该没问题了!
#13
在这个cpp同名的.h文件中,添加上另一个cpp同名的.h引入后,声明一下你要调用的对象,应该就可以调用了。
#14
好的,我试试,谢谢