3

So I'm 100% new to ruby qt, I'm used to python, and I got the basics but because the QMenuBar wont align properly with just Qt::Widget I have been trying to figure out the QMainWindow command. I know its like 1000% easy the second you tell me but the .setCentralWidget command doesnt seem to work for ruby so I'm just lost...

Here is my "testing" code if you get the question you really dont need this but if I dont put it up some one wil be like wherreee yourrr codddee!!! I dont get it?

    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
  • I had tryed to implement it before but all I could get was an error message saying "Attempting to set QLayout "" on Window "", which already has a layout." Thats why I posted my before Qt::MainWindow attempt was made. – Max Iglehart Mar 06 '13 at 15:54

2 Answers2

2

Found a great template, hope this helps any fellow newbies who hate the ruby documentation.

    require 'Qt4'
    class Ui_MainWindow

          def setupUi(mainWindow)    
          mainWindow.setObjectName("mainWindow")    
          @font = Qt::Font.new    
          @font.setPointSize(14)    
          mainWindow.setFont(@font)    
          @centralwidget = Qt::Widget.new(mainWindow)    
          @centralwidget.setObjectName("centralwidget")    
          @qgridLayout = Qt::GridLayout.new(@centralwidget)    
          @qgridLayout.setObjectName("qgridLayout")    
          @label = Qt::Label.new(@centralwidget)    
          @label.setObjectName("label")    
          @label.setAlignment(Qt::AlignCenter)    
          @qgridLayout.addWidget(@label, 0, 0, 1, 1)   
          mainWindow.setCentralWidget(@centralwidget)    
          @menubar = Qt::MenuBar.new(mainWindow)    
          @menubar.setObjectName("menubar")    
          @menubar.setGeometry(Qt::Rect.new(0, 0, 517, 35))    
          mainWindow.setMenuBar(@menubar)    
          @statusbar = Qt::StatusBar.new(mainWindow)    
          @statusbar.setObjectName("statusbar")    
          mainWindow.setStatusBar(@statusbar)    
          @dockWidget = Qt::DockWidget.new(mainWindow)    
          @dockWidget.setObjectName("dockWidget")    
          @dockWidget.setAutoFillBackground(false)    
          @dockWidget.setAllowedAreas(Qt::TopDockWidgetArea)    
          @dockWidgetContents = Qt::Widget.new(@dockWidget)    
          @dockWidgetContents.setObjectName("dockWidgetContents")    
          @qgridLayout1 = Qt::GridLayout.new(@dockWidgetContents)    
          @qgridLayout1.setObjectName("qgridLayout1")    
          @label_2 = Qt::Label.new(@dockWidgetContents)    
          @label_2.setObjectName("label_2")    
          @label_2.setAlignment(Qt::AlignCenter)    
          @qgridLayout1.addWidget(@label_2, 0, 0, 1, 1)    
          @dockWidget.setWidget(@dockWidgetContents)    
          mainWindow.addDockWidget(4, @dockWidget)    
          @dockWidget_2 = Qt::DockWidget.new(mainWindow)    
          @dockWidget_2.setObjectName("dockWidget_2")    
          @dockWidget_2.setAllowedAreas(Qt::LeftDockWidgetArea)    
          @dockWidgetContents_2 = Qt::Widget.new(@dockWidget_2)    
          @dockWidgetContents_2.setObjectName("dockWidgetContents_2")    
          @qgridLayout2 = Qt::GridLayout.new(@dockWidgetContents_2)    
          @qgridLayout2.setObjectName("qgridLayout2")    
          @label_3 = Qt::Label.new(@dockWidgetContents_2)    
          @label_3.setObjectName("label_3")    
          @label_3.setAlignment(Qt::AlignCenter)    
          @qgridLayout2.addWidget(@label_3, 0, 0, 1, 1)    
          @dockWidget_2.setWidget(@dockWidgetContents_2)    
          mainWindow.addDockWidget(1, @dockWidget_2)    
          @dockWidget_3 = Qt::DockWidget.new(mainWindow)    
          @dockWidget_3.setObjectName("dockWidget_3")    
          @dockWidget_3.setAllowedAreas(Qt::RightDockWidgetArea)    
          @dockWidgetContents_3 = Qt::Widget.new(@dockWidget_3)    
          @dockWidgetContents_3.setObjectName("dockWidgetContents_3")    
          @qgridLayout3 = Qt::GridLayout.new(@dockWidgetContents_3)    
          @qgridLayout3.setObjectName("qgridLayout3")    
          @label_4 = Qt::Label.new(@dockWidgetContents_3)    
          @label_4.setObjectName("label_4")    
          @label_4.setAlignment(Qt::AlignCenter)    
          @qgridLayout3.addWidget(@label_4, 0, 0, 1, 1)    
          @dockWidget_3.setWidget(@dockWidgetContents_3)    
          mainWindow.addDockWidget(2, @dockWidget_3)    
          @dockWidget_4 = Qt::DockWidget.new(mainWindow)    
          @dockWidget_4.setObjectName("dockWidget_4")    
          @dockWidget_4.setAllowedAreas(Qt::BottomDockWidgetArea)    
          @dockWidgetContents_4 = Qt::Widget.new(@dockWidget_4)    
          @dockWidgetContents_4.setObjectName("dockWidgetContents_4")    
          @qgridLayout4 = Qt::GridLayout.new(@dockWidgetContents_4)    
          @qgridLayout4.setObjectName("qgridLayout4")    
          @label_5 = Qt::Label.new(@dockWidgetContents_4)    
          @label_5.setObjectName("label_5")    
          @label_5.setAlignment(Qt::AlignCenter)    
          @qgridLayout4.addWidget(@label_5, 0, 0, 1, 1)    
          @dockWidget_4.setWidget(@dockWidgetContents_4)   
          mainWindow.addDockWidget(8, @dockWidget_4)    
          retranslateUi(mainWindow);    
          size = Qt::Size.new(517, 391)    
          size = size.expandedTo(mainWindow.minimumSizeHint())    
          mainWindow.resize(size)    
          Qt::MetaObject.connectSlotsByName(mainWindow)    
          end 
          # setupUi    
          def retranslateUi(mainWindow)    
          mainWindow.setWindowTitle(Qt::Application.translate("MainWindow", "Example 2", nil, Qt::Application::UnicodeUTF8))    
          @label.setText(Qt::Application.translate("MainWindow", "Central Widget", nil, Qt::Application::UnicodeUTF8))    
          @label_2.setText(Qt::Application.translate("MainWindow", "Top Docking Area", nil, Qt::Application::UnicodeUTF8))    
          @label_3.setText(Qt::Application.translate("MainWindow", "Left Docking Area", nil, Qt::Application::UnicodeUTF8))    
          @label_4.setText(Qt::Application.translate("MainWindow", "Right Docking Area", nil, Qt::Application::UnicodeUTF8))    
          @label_5.setText(Qt::Application.translate("MainWindow", "Bottom Docking Area", nil, Qt::Application::UnicodeUTF8))    
          end 
    # retranslateUi
    end
    module Ui    
          class MainWindow < Ui_MainWindow    
          end
    end  
    # module Ui
    if $0 == __FILE__    
            a = Qt::Application.new(ARGV)    
            u = Ui_MainWindow.new
            w = Qt::MainWindow.new
            u.setupUi(w)
            w.show
            a.exec
    end
0

So the thing that is a little confusing with setCentralWidget is that you need to pass it a Qt::Widget and passing it a Qt::Layout will give you a NoMethodError. It seems a little counter intuitive I think because most people want to setup a grid, hbox, or vbox layout for their first basic GUI.

Once you set your widget as the central widget you can call

yourCentralWidget.layout = yourNewLayout

and that set whatever layout you want as the main window. I hope that helps!

jgelderloos
  • 378
  • 4
  • 19