gpt4 book ai didi

qteSTLib - QTest::mouse的问题单击QListWidget

转载 作者:行者123 更新时间:2023-12-04 11:41:55 27 4
gpt4 key购买 nike

我正在尝试使用QTest进行一些测试。我有一个QListWidget,我想单击以获取选择。但是单击之后,什么也没有选择。有人有什么想法吗?

这是我的考试课

void TestGui::List() {
TestDialog dlg;
dlg.show ();

// Click on the centre of the second object
QListWidget *list = dlg.ListWidget ();
QListWidgetItem *item = list->item ( 1 );
QRect rect = list->visualItemRect ( item );
QTest::mouseClick ( list, Qt::LeftButton, 0, rect.center() );

// Check if something was selected
QCOMPARE ( list->currentRow (), 1 );
QVERIFY ( list->currentItem () != NULL );
QCOMPARE ( list->currentItem ()->text (), QString ( "Two" ) );
}

下面是测试课
class TestGui: public QObject {
Q_OBJECT

private slots:
void List();
};

这是用于显示问题的TestDialog类
class TestDialog : public QDialog {
Q_OBJECT

public:
TestDialog ( QWidget *parent = NULL )
: QDialog ( parent, Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint ) {
QVBoxLayout *layout = new QVBoxLayout ( this );

m_list = new QListWidget ( this );
m_list->addItem ( "One" );
m_list->addItem ( "Two" );
m_list->addItem ( "Three" );
m_list->addItem ( "Four" );
layout->addWidget ( m_list );

QPushButton *close_button = new QPushButton( "Close" );
connect ( close_button, SIGNAL ( clicked () ), this, SLOT ( close () ) );
layout->addWidget ( close_button );

setWindowTitle( "Test" );
}

QListWidget *ListWidget ( void ) {
return m_list;
};

private:
QListWidget *m_list;

}; // TestDialog

最佳答案

经过一番思考之后,事实证明,单击应该位于 View 小部件上,而不是列表本身。所以这条线应该看起来像这样

QTest::mouseClick(list-> viewport(),Qt::LeftButton,0,rect.center());

谢谢

关于qteSTLib - QTest::mouse的问题单击QListWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10728362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com