Is it possible to open an attribute table from the Python console or through a Python script in QGIS?
PyQGIS – How to Open Attribute Table Using QGIS Python Console
pyqgis
Related Solutions
from PyQt4.QtGui import QApplication, QTableView
# Get a list of all open attribute table dialogs
attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName() == u'QgsAttributeTableDialog' or d.objectName() == u'AttributeTable' ]
# Set the column width for the first column in the first dialog to 200
attrTables[0].findChildren(QTableView)[0].setColumnWidth( 0, 200 )
# or instead set the width to match the contents maximum size
# attrTables[0].findChildren(QTableView)[0].resizeColumnsToContents()
To automatically open the QGIS Python Console when QGIS starts, you can read How to show Python console at QGIS program start.
However, I guess your ultimate intention is to run the script (even if it doesn't run from the QGIS Python Console), so you can create (if it doesn't exist already) a startup.py
file in the QGIS Python directory:
- On GNU/Linux:
/home/USER/.qgis2/python/
- On Windows:
C:\Program Files\QGIS\python\
orC:\Documents and Settings\$USERNAME\.qgis\python\
(I don't really know about paths on Windows 10, but if you have problems finding the correct Python path, just open the Plugin manager, click on any installed 3rd party plugin and read the path where it's installed).
And then either:
Call your script from
startup.py
:import my_script my_script.run() # If that's the method you want to execute from your script
or,
Just put your script code into the
startup.py
file, which is the same as copying your Python script to the aforementioned folder and rename it tostartup.py
EDIT: Explain caveat when using the startup.py approach and suggest another approach
startup.py
will be executed everytime you open QGIS, which gives you no control on whether you want to automatically run your script or just run QGIS to work on a new project.
Therefore, I suggest you to take another approach: use QGIS project macros.
Say you have a script (myScript.py
) you want to run when QGIS starts from your schedule tasks. Copy/move the script into the QGIS Python path or into the same directory where your QGIS project will be saved.
Let's assume your script looks like this:
from qgis.utils import iface
def run():
iface.addVectorLayer("/Path/To/geodata/myShape.shp","layer1","ogr")
Open a new QGIS project and click Project-->Project Properties...-->Macros
, enable the Python macros
checkbox, and write this:
import myScript
def openProject():
myScript.run()
def saveProject():
pass
def closeProject():
pass
Click OK and go to Settings-->Options...-->General
to choose Enable macros Always
.
Save your QGIS project as, let's say, run_script_project.qgs
.
Now, every time you want to get your script automatically executed when starting QGIS, just start QGIS from the console, in this way:
qgis --project /Path/To/Your/Project/run_script_project.qgs
Once QGIS opens the project, the run()
method of your script will be executed.
Best Answer
Yes you can, at least with the latest master version and 1.8. It is a method exposed via iface
You will need a preexisting reference to a QgsVectorLayer.
For direct editing of features it is also a method exposed via iface:
You will need a preexisting reference to a QgsVectorLayer and a QgsFeature in both cases. The feature form opened and whether it is editable are dependent upon app settings and whether the feature's layer is in edit mode.