【发布时间】:2013-03-06 15:52:28
【问题描述】:
所以我对 ruby qt 是 100% 的新手,我已经习惯了 python,而且我已经掌握了基础知识,但是因为 QMenuBar 不能与 Qt::Widget 正确对齐,所以我一直在尝试找出 QMainWindow 命令。我知道你告诉我的那一秒就像 1000% 容易,但是 .setCentralWidget 命令似乎不适用于 ruby,所以我只是迷路了......
这是我的“测试”代码,如果你遇到问题,你真的不需要这个,但如果我不把它放出来,有些人会像 wherreee yourrr codddee 一样!!!没看懂?
require 'Qt'
class Window < Qt::Widget
slots 'slotAbout()', 'slotExit()', 'slotNew()'
def initialize(parent = nil)
super(parent)
#menubar part
@menubar = Qt::MenuBar.new(self)
@menubar.setObjectName('menubar')
@menuFile = Qt::Menu.new(@menubar)
@menuFile.setObjectName('menuFile')
@menuFile.setTitle('File')
@menuHelp = Qt::Menu.new(@menubar)
@menuHelp.setObjectName('menuHelp')
@menuHelp.setTitle('&Help')
@actionNew = Qt::Action.new(self)
@actionNew.setObjectName('actionNew')
@actionNew.setText('New')
@actionExit = Qt::Action.new(self)
@actionExit.setObjectName('actionExit')
@actionExit.setText('Exit')
@actionAbout = Qt::Action.new(self)
@actionAbout.setObjectName('actionAbout')
@actionAbout.setText('About')
@menubar.addAction(@menuFile.menuAction())
@menubar.addAction(@menuHelp.menuAction())
@menuFile.addAction(@actionNew)
@menuFile.addAction(@actionExit)
@menuHelp.addAction(@actionAbout)
@text = Qt::LineEdit.new
@textbox.setStyleSheet "QWidget { color : #f52c01 }"
@textbox.setStyleSheet "QWidget { color : #009600 }"
@text.setStyleSheet "QWidget { color : black }"
@text.setText(tr("Somthing"))
connect(@actionExit, SIGNAL('triggered()'), self, SLOT('slotExit()'))
connect(@actionNew, SIGNAL('triggered()'), self, SLOT('slotNew()'))
connect(@actionAbout, SIGNAL('triggered()'), self, SLOT('slotAbout()'))
#causes some bugs
# statusbar = Qt::StatusBar.new(self)
# statusbar.setObjectName('statusbar')
#examples of groups
oneGroup = Qt::GroupBox.new(tr("GroupOne"))
oneLabel = Qt::Label.new(tr("One"))
twoGroup = Qt::GroupBox.new(tr("GroupTwo"))
twoLabel = Qt::Label.new(tr("One, I mean two!"))
twoslider = Qt::Slider.new(Qt::Horizontal)
twoslider.setRange(0, 5)
twoslider.setValue(0)
#connecting objects to slots
# connect(onething, SIGNAL('thing(int)'), self, SLOT('slotAbout( int )'))
#layout set up using groups
oneLayout = Qt::GridLayout.new
oneLayout.addWidget(oneLabel, 0, 0)
oneLayout.addWidget(@text, 1, 0)
oneGroup.layout = oneLayout
twoLayout = Qt::GridLayout.new
twoLayout.addWidget(twoLabel, 0, 0)
twoLayout.addWidget(twoslider, 1, 0)
twoGroup.layout = twoLayout
layout = Qt::VBoxLayout.new
layout.addWidget(@menubar)
layout.addWidget(oneGroup)
layout.addWidget(twoGroup)
setLayout(layout)
setWindowTitle(tr("Main Title"))
end
def slotExit()
app.exec
end
def slotAbout()
Qt::MessageBox.about(self, tr("About Recent Files"), tr("Weeeaaalllll thar theaes fear-es that make thes that which happen, well just karda happen."))
end
def slotNew()
other = Window.new
other.show()
end
end
app = Qt::Application.new(ARGV)
window = Window.new
window.show
app.exec
【问题讨论】:
-
我之前尝试过实现它,但我得到的只是一条错误消息,上面写着“正在尝试在窗口“”上设置 QLayout“”,它已经有一个布局。”这就是我在 Qt::MainWindow 尝试之前发布的原因。
标签: ruby user-interface qt4 qtruby