对于我的GUI,我希望有两对按钮可以向上和向下滚动滚动条。第一组按钮应该在说scrollarea1上工作,第二组按钮应该在scrollarea2上工作。我放在scrollarea中的小部件称为viewport1和viewport2。 由于两组按钮都应该相同(向上和向下滚动),我想我会创建两个名为scrollUp和scrollDown的插槽来处理两组按钮的滚动。不幸的是,我无法完成这项工作,需要一些帮助。我尝试过以下方法:
QPushButton up;
QPushButton down;
QPushButton up2;
QPushButton down2;
connect(&up,SIGNAL(clicked()),&up,SLOT(scrollUp()));
connect(&up2,SIGNAL(clicked()),&up,SLOT(scrollUp()));
connect(&down,SIGNAL(clicked()),&down,SLOT(scrollDown()));
connect(&down2,SIGNAL(clicked()),&down,SLOT(scrollDown()));
void MainWindow::scrollDown()
{
QScrollArea area;
QWidget view;
if((QPushButton) &sender = down)
{
area=scrollArea;
view=viewport;
}
if((QPushButton) &sender = down2)
{
area=scrollArea;
view=viewport;
}
int curpos = area.verticalScrollBar()->value();
area.verticalScrollBar()->setValue(curpos+15);
int newpos = area.verticalScrollBar()->value();
QPoint topLeft = area.viewport()->rect().topLeft();
view.move(topLeft.x(),topLeft.y()-(newpos));
}
void MainWindow::scrollUp()
{
QScrollArea area;
QWidget view;
if((QPushButton) &sender = up)
{
area=scrollArea;
view=viewport;
}
if((QPushButton) &sender = up2)
{
area=scrollArea2;
view=viewport2;
}
int curpos = area.verticalScrollBar()->value();
area.verticalScrollBar()->setValue(curpos-15);
int newpos = area.verticalScrollBar()->value();
QPoint topLeft = area.viewport()->rect().topLeft();
view.move(topLeft.x(),topLeft.y()-(newpos));
}
但这不起作用有几个原因。我也尝试给插槽一些参数,例如:
connect(&up,SIGNAL(clicked()),&up,SLOT(scrollUp(scrollarea1,viewport1)));
connect(&up2,SIGNAL(clicked()),&up,SLOT(scrollUp(scrollarea2,viewport2)));
但同样,没有成功。有人能帮助我吗?
答案 0 :(得分:7)
首先,“它不起作用”并不意味着什么,如果你没有说出你得到的错误,很难帮助你。然后,几乎没有问题。
所有QObject的派生类都不可复制,这意味着你无法做到
QWidget a;
QWidget b;
b = a; // Wrong
你应该使用指针(或者可能是引用)。
QWidget a;
QWidget * b = new QWidget(...);
QWidget * c;
c = & a; // Ok
c = b; // Ok
然后您的connect
来电错误:
connect(&up, SIGNAL(clicked()), &up, SLOT(scrollUp()));
第三个参数是拥有插槽的对象。 up
是一个QPushButton,它没有scrollUp()
个广告位,是你的MainWindow做的:
connect(&up, SIGNAL(clicked()), this, SLOT(scrollUp()));
(因为在MainWindow的构造函数connect
中调用this
指向当前的MainWindow对象。)
同样在C ++中,单=
符号表示赋值,对于相等比较,使用=='. And
sender`是一个函数。
如果以正确的方式实施,您的方法应该有效:
class MainWindow: public QWidget
{
QScrollArea * scroll1;
QScrollArea * scroll2;
QWidget * view1;
QWidget * view2;
QPushButton * up1;
QPushButton * up2;
QPushButton * down1;
QPushButton * down2;
public:
MainWindow()
{
// Here initialize member variables.
...
connect(up1, SIGNAL(clicked()), this, SLOT(scrollUp()));
connect(up2, SIGNAL(clicked()), this, SLOT(scrollUp()));
connect(down1, SIGNAL(clicked()), this, SLOT(scrollDown()));
connect(down2, SIGNAL(clicked()), this, SLOT(scrollDown()));
}
public slots:
void scrollDown()
{
QScrollArea * area;
QWidget * view;
if(qobject_cast<QPushButton>(sender()) == down1) {
area = & scroll1;
view = & view1;
} else if(qobject_cast<QPushButton>(sender()) == down2) {
area = & scroll2;
view = & view2;
} else {
// Error.
}
// Now `area` and `view` point to the right widgets.
...
}
void scrollUp()
{
// The same as before.
}
};
另一种方法是将实际滚动指令提取到单独的函数中:
class MainWindow: public QWidget
{
// Same variables as before
...
public:
MainWindow()
{
// Here initialize member variables.
...
connect(up1, SIGNAL(clicked()), this, SLOT(scrollUp1()));
connect(up2, SIGNAL(clicked()), this, SLOT(scrollUp2()));
connect(down1, SIGNAL(clicked()), this, SLOT(scrollDown1()));
connect(down2, SIGNAL(clicked()), this, SLOT(scrollDown2()));
}
public slots:
void scrollDown(QScrollArea * area, QWidget * view)
{
// Here you scroll over `area` and `view`.
}
void scrollDown1()
{
scrollDown(scroll1, area1);
}
void scrollDown2()
{
scrollDown(scroll2, area2);
}
// Again, the same for `scrollUp`.
};
答案 1 :(得分:5)
您的代码中存在多个错误:
QObject
”的sender
,而是方法QObject * QObject::sender() const;
,它会在信号的发送方返回一个指针。QPushButton**
投射到QPushButton
((QPushButton) &sender
)中,并且您不会将该事物与按钮up(2)
和{{1}进行比较}。down(2)
和scrollUp
广告位不属于scrollDown
类,而属于QPushButton
类。最后,你应该写下这样的东西:
MainWindow
答案 2 :(得分:0)
首先,插槽除了信号之外没有其他参数。 Clicked没有参数,因此插槽没有参数。
我认为检查scrollArea 1或2是否具有焦点并决定哪一个应该移动的最简单方法。
我还认为您的代码中存在错误。不应该这样:
if((QPushButton) &sender = down2)
{
area=scrollArea;
view=viewport;
}
是这样的:
if((QPushButton) &sender = down2)
{
area=scrollArea2;
view=viewport2;
}
答案 3 :(得分:0)
首先,这是伪代码。它不会编译,但它应包含必要的信息。
我相信使用QSignalMapper类可以最优雅地解决这个问题。它允许来自多个发送器的无参数信号连接到一个插槽。
在标题中,写下这样的内容:
class QSignalMapper;
class MainWindow : public QMainWindow
{
public:
void init();
public slots:
void handleScrollButtons(int id);
private:
enum { ScrollUp1, ScrollDown1, ScrollUp2, ScrollDown2 } // just makes it more convenient to use
QSignalMapper *m_scrollbuttonhandler;
}
在源文件中,写下这样的内容
#include <QSignalMapper>
void MainWindow::init()
{
m_scrollbuttonhandler = new QSignalMapper(this);
m_scrollbuttonhandler->setMapping(scrollup1button, ScrollUp1);
m_scrollbuttonhandler->setMapping(scrolldown1button, ScrollDown1);
m_scrollbuttonhandler->setMapping(scrollup2button, ScrollUp2);
m_scrollbuttonhandler->setMapping(scrolldown2button, ScrollDown2);
connect(scrollup1button, SIGNAL(clicked(bool)), m_scrollbuttonhandler, SLOT(map()));
connect(scrolldown1button, SIGNAL(clicked(bool)), m_scrollbuttonhandler, SLOT(map()));
connect(scrollup2button, SIGNAL(clicked(bool)), m_scrollbuttonhandler, SLOT(map()));
connect(scrolldown2button, SIGNAL(clicked(bool)), m_scrollbuttonhandler, SLOT(map()));
connect(m_scrollbuttonhandler, SIGNAL(mapped(int)), this, SLOT(handleScrollButtons(int)));
}
void MainWindow::handleScrollButtons(int id)
{
switch (id)
{
case ScrollUp1:
// stuff to do for scrollup1button
case ScrollDown1:
// stuff to do for scrolldown1button
case ScrollUp2:
// stuff to do for scrollup2button
case ScrollDown2:
// stuff to do for scrolldown2button
}
}