我想用Qt 5.1实现一个带有自定义角色的可排序TableView.但是,当用户点击标题时,我不知道该怎么做才能对它进行排序.
在我的Qt .pro文件中,我补充说:
!android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets
Run Code Online (Sandbox Code Playgroud)
在main.cpp中,我用作QtWidgets/QApplication全局app实例,并使用qmlRegisterType作为我的新模型类(见下文):
qmlRegisterType<PositionModel>("MyDataModule", 1, 0, "PositionModel");
Run Code Online (Sandbox Code Playgroud)
PositionModel声明如下:
class PositionModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum PositionRoles {
CustomRol1 = Qt::UserRole + 1,
CustomRow2,
PositionRoleMaxPlus1
};
explicit PositionModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, …Run Code Online (Sandbox Code Playgroud) 背景故事
所以我最近决定尝试Qt.我开始制作QtQuick Apllication.在我的设计师视图中,我有一个按钮和一个鼠标区域.
我想做的事:
当我单击按钮时,我想显示一个带有一些文本的消息框(如"Hello World").
我的问题
我怎样才能做到这一点 ?
附加信息
我试着谷歌搜索它,我试着按照这个答案.但仍然没有.我知道如何在.Net(C#和VB)中编程,我在C/C++中有一些知识,但Qt对我来说似乎很难
我使用 QQuickView::beforeRendering 事件在 qml 控件下渲染我的 3d 模型。如果用户在任何 qml 控件之外单击,我想在 C++ 中处理鼠标事件/如何在 QQuickView::mousePressEvent 中发现鼠标在 qml 控件之外被按下?
我想要的外观是有一个纯色按钮,文本就像"Hello World",文本完全透明,背景通过按钮显示.
换句话说,将文本作为按钮元素上的透明蒙版.
如果我使用滑动视图,我会遇到一些问题,并且我编写了以下代码:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Swipe View")
MainForm {
anchors.fill:parent
Rectangle{
id:rightRect
anchors.right:parent.right
width: parent.width*0.50
height:parent.height
color:"yellow"
}
Rectangle{
id:leftRect
width:parent.width*0.50
height:parent.height
color:"lightgreen"
border.color:"red"
anchors.right:rightRect.left
SwipeView{
id:swipeView
anchors.fill : leftRect
//Layout.fillWidth: true
currentIndex: 0
interactive: false
Page{
id:page1
Rectangle{
width:parent.width
height:parent.height
color:"lightgreen"
Button{
text:"move to 2"
onClicked: swipeView.currentIndex = 1
}
}
}
Page{
id:page2
Rectangle{
width:parent.width
height:parent.height
color:"lightblue"
Button{
text:"move to 1"
onClicked: swipeView.currentIndex = …Run Code Online (Sandbox Code Playgroud) 我已经安装了Qt 4.5.0社区,我正在尝试使用Qt5.10.0 MinGW 32位构建我的项目。将QtQuick 2.12导入到我的qtquick项目的qml文件中后,我遇到以下错误:
未安装模块“ QtQuick” 2.12版
值得一提的是,当我尝试编写QtQu的第一个字符时,此模块是建议的模块之一。
如果能帮助我如何克服这一问题,将不胜感激
在Listview中,我使用“ delegate ”弹出了 100 个项目,假设 listview 已经显示了填充值。现在我想从 C++ 中提取 QML 列表视图中已显示的值。如何实现这一目标?注意: 我无法直接访问数据模型,因为我正在使用隐藏变量在委托中进行过滤
/*This is not working code, Please note,
delegate will not display all model data.*/
ListView
{
id:"listview"
model:datamodel
delegate:{
if(!hidden)
{
Text{
text:value
}
}
}
//Can I access by using given approach?
QObject * object = m_qmlengine->rootObjects().at(0)->findChild<QObject* >("listview");
//Find objects
const QListObject& lists = object->children();
//0 to count maximum
//read the first property
QVarient value = QQmlProperty::read(lists[0],"text");
Run Code Online (Sandbox Code Playgroud) 我希望能够Flickable使用鼠标滚轮(或触摸板上的两根手指)滚动,而不更改它可能包含的Slider内容。
示例代码及结果应用:
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
ApplicationWindow {
id: rootWindow
visible: true
width: 400
height: 200
title: qsTr("Hello World")
ScrollView {
anchors.fill: parent
flickableItem.flickableDirection: Flickable.VerticalFlick
Column {
Repeater {
model: 40
Slider {
width: rootWindow.width * 0.9
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
看起来过去曾尝试修复此问题,但没有成功。
编辑:这仅涉及Controls 1.x,因为从 2.0 版本开始,控件似乎没有这个问题。
我试图在按下按钮时动态地将 tabButton 添加到TabBar但我花了很多时间搜索但我不知道如何添加,下面是我正在处理的代码:
我的选项卡按钮.qml
import QtQuick 2.4
import QtQuick.Controls 2.2
Item
{
property int BtnWidth:0
property int BtnHeight:0
property string BtnText: ""
property bool isChecked : false
TabButton
{
id:tabBtn
text:BtnText
width:BtnWidth
height:BtnHeight
}
}
Run Code Online (Sandbox Code Playgroud)
MainForm.qml
import QtQuick 2.4
import QtQuick.Controls 2.2
Rectangle
{
Button
{
id:button
width:100
height:100
anchors.top:parent.top
text:qStr("Add")
onClicked{
//How to add logic here to add tab in below tabBar.
}
}
TabBar
{
id:tabBar
anchors.top:button.bottom
width:500
height:500
}
}
Run Code Online (Sandbox Code Playgroud) 我试图Menu从 a 动态填充 a ListModel,但这种方法不起作用(当我右键单击菜单时不会显示任何内容):
这是我的菜单项:
import QtQuick.Controls 1.3
ListModel{
id:menuItems
ListElement{
text:"hello1"
}
ListElement{
text:"hello2"
}
ListElement{
text:"hello3"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的菜单
Menu{
id:contextMenu
Repeater{
model: menuItems
MenuItem{}
}
Run Code Online (Sandbox Code Playgroud)
我什至尝试添加一个Instantiator但菜单不会显示任何内容